iOS didFinishLaunchingWithOptions: no URL 虽然应用是通过点击 link 打开的

iOS didFinishLaunchingWithOptions: no URL although the app is opened by tapping a link

我正在使用 Branch 处理的 link 打开我的应用程序:

https://my.link?param=test

didFinishLaunchingWithOptions中:

Branch.getInstance()?.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: { params, error in
    if error == nil {
        if let param = params?["param"] as? String {
            print(param)
        }
    }
}

这会在控制台中正确打印 test

但是,我之前也尝试获取url,但是尝试获取launchOptions?[.url],结果我得到了nil

那么我怎样才能得到 url?感谢您的帮助。

您需要使用UIApplicationLaunchOptionsKey

试试这个:

if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? URL {
  // Your code
}
else if let activityDictionary = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [AnyHashable: Any] { //Universal link
    for key in activityDictionary.keys {
        if let userActivity = activityDictionary[key] as? NSUserActivity {
               if let url = userActivity.webpageURL {
                    // your code
            }
        }
    }
}