实际上,关于的答案UIApplicationLaunchOptionsURLKey
是正确的,但并不完整。例如,如果用户在消息应用程序中点击通用链接并被重定向到您的应用程序,您会收到这些启动选项,而不是UIApplicationLaunchOptionsURLKey
:
[
UIApplicationLaunchOptionsSourceApplicationKey: com.apple.MobileSMS,
UIApplicationLaunchOptionsUserActivityDictionaryKey: [
UIApplicationLaunchOptionsUserActivityKey: <NSUserActivity>,
UIApplicationLaunchOptionsUserActivityTypeKey: NSUserActivityTypeBrowsingWeb
]
]
因此,要检查用户是否从 URL 登陆应用程序,在这种情况下您需要此代码:
let isFromUrl = ((launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? NSDictionary)?[UIApplicationLaunchOptionsKey.userActivityType] as? String == NSUserActivityTypeBrowsingWeb)
如果检查通过,则处理传入的 URL:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let url = userActivity.webpageURL
// do your stuff ...
}
return false
}