唯一重要的部分是這樣的部分:
// 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中處理它,或者根據需要將它路由到另一個類。