从某些URL AppDelegates's
打开iOS应用程序时,按以下顺序调用方法:
1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
如果从URL打开应用程序,如何知道didFinishLaunchingWithOptions
方法。可能有一些我想念的发布选项?
答案 0 :(得分:9)
您可以检查传递给launchOptions
的{{1}}。
在参考docs中查找- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
部分,特别是Launch Options Keys
答案 1 :(得分:6)
如果您的应用已从网址启动,您会找到
UIApplicationLaunchOptionsURLKey
<{1}}字典中的 - launchOptions
在相关说明中,不推荐使用(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法,您应该使用:
handleOpenURL:
答案 2 :(得分:0)
首先,您应该实施application:didFinishLaunchingWithOptions:
检查URL。如果你可以打开它应该返回YES,否则你应该返回NO。
然后实施application:handleOpenURL:
打开URL。成功时应返回YES或NO。
答案 3 :(得分:0)
关于UIApplicationLaunchOptionsURLKey
的实际答案是正确的,但并不完整。例如,如果用户在Messages应用程序中点击Universal Link并将其重定向到您的应用程序中,则会收到以下启动选项,而不是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
}