我花了一些时间来解决这个问题。在尝试了一堆解决方法之后,有效的解决方案是迁移到新的Apple's App Structure
using SceneDelegate
.
要从终止状态处理 DeepLink,您需要配置此功能:
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = connectionOptions.userActivities.first, let url = userActivity.webpageURL {
DynamicLinks.dynamicLinks().handleUniversalLink(url) { dynamicLink, error in
guard let dynamicLink = dynamicLink, let url = dynamicLink.url else { return }
parseDynamicLink(fromURL: url)
}
}
guard let windowScene = (scene as? UIWindowScene) else { return }
// Initialize UIWindow
window = UIWindow(windowScene: windowScene)
window?.rootViewController = YOUR_ROOT_VIEW_CONTROLLER
window?.makeKeyAndVisible()
}
要处理与 UIApplication 等效的场景委托的其他部分:
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
guard let url = userActivity.webpageURL else { return }
DynamicLinks.dynamicLinks().handleUniversalLink(url) { dynamicLink, error in
guard let dynamicLink = dynamicLink, let url = dynamicLink.url else { return }
parseDynamicLink(fromURL: url)
// launch dynamic link code here...?
}
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let urlContext = URLContexts.first else { return }
if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: urlContext.url) {
guard let url = dynamicLink.url else { return }
parseDynamicLink(fromURL: url)
}
}
如果您打算进行迁移,请确保正确配置您Info.plist
的 'sApplication Scene Manifest
然后在此处设置您的场景配置:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
你Application Scene Manifest
应该看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
</dict>
</array>
</dict>
</dict>
</plist>