唯一重要的部分是这样的部分:
// Add at end of application:didFinishLaunchingWithOptions
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (url != nil && [url isFileURL]) {
[rootController handleOpenURL:url];
}
// Add new method
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url {
RootViewController *rootController = (RootViewController *) [navigationController.viewControllers objectAtIndex:0];
if (url != nil && [url isFileURL]) {
[rootController handleOpenURL:url];
}
return YES;
}
第一个代码块添加到您的AppDelegate的application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
雨燕相当于是
if let options = launchOptions, let url = options[.url] as? URL, url.isFileURL {
// call some code to handle the URL
}
,并为这个新功能AppDelegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.isFileURL {
// call some code to handle the URL
}
return true // if successful
}
本文中的所有其他代码都是将处理代码路由到根视图控制器的一种方式。你可以直接在AppDelegate中处理它,或者根据需要将它路由到另一个类。