SceneDelegate 中未调用通用链接回调函数

Sam*_*med 5 deep-linking ios swift ios-universal-links swift3

根据我的应用程序项目设置,\n我使用相同的代码进行以下函数调用,以分别在 SceneDelegate 和 AppDelegate 中实例化 rootVC

\n
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {\n\n}\n\xe2\x80\xa8func application(_ application: UIApplication,\n                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?)\n        -> Bool {\n}\xe2\x80\xa8\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa8\xe2\x80\xa8为了实现通用链接,我在我的 App Delegate\xe2\x80\xa8\xe2\x80\xa8 中有以下回调函数

\n
func application(_ application: UIApplication,\n                     continue userActivity: NSUserActivity,\n                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {\n//code to capture and setup universal link\n}\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa8AppDelegate 中的这个函数仅在低于 iOS 13 的设备中调用。我寻找了 SceneDelegate 的类似回调等效项,我能找到的最接近的是这个函数。\xe2\x80\xa8\xe2\x80\xa8

\n
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {\n//code to capture and setup universal link\n}\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa8配置:Xcode 版本 11.5 目标 iOS 10+ 设备。\n\xe2\x80\xa8问题:仅当单击链接之前有应用程序实例正在运行时才会调用此特定回调。即一旦应用程序实例被杀死,来自 SceneDelegate 的这个函数就不会被调用,并且通用链接不适用于 iOS13+ 设备。\xe2\x80\xa8\xe2\x80\xa8I 尝试按照此 Xcode 11 - Opt out of UISceneDelegate/SwiftUI on iOS 13完全删除了场景委托,但最终只有黑屏。\n问题:我做错了什么以及可能的修复方法是什么?\xe2\x80\xa8\xe2\x80\xa8

\n

Mik*_*num 1

我遇到了同样的问题,问题是您有一个 SceneDelegate,因此不会调用 AppDelegate 方法。因此,您的 SceneDelegate 中缺少一个方法,您留空的第一个方法将在应用程序尚未启动时处理通用链接。

在您的 SceneDelegate 中实现以下方法,以在应用程序已运行和尚未启动时处理通用链接:

//This method is called when app is NOT running in the background.
//Easy to test with fatalError()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    
    if let userActivity = connectionOptions.userActivities.first {
        debugPrint("userActivity: \(userActivity.webpageURL)")
        fatalError()
    }
}

//This method is called when app is running in the background.
//Easy to test with debugPrint
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    
    debugPrint("userActivity: \(userActivity.webpageURL)")
}
Run Code Online (Sandbox Code Playgroud)

从那里,做任何你需要处理链接的事情。

希望能帮助到你 :)