应用程序关闭时动态链接不起作用,仅在后台运行

Dynamic Links not working when app is closed, only when in background

我一直在努力在我的应用程序中包含动态链接。我已将其设置为 links 可以正确生成并正确接收,但它们仅在应用程序打开并处于后台时才有效。如果应用程序完全关闭,link 只会打开应用程序。看起来它没有在 AppDelegate 中调用 continueUserActivity 方法,即使我在 didFinishLaunchingWithOptions 方法中返回 TRUE。我读过我应该在 didFinishLaunchingWithOptions 方法中获取传入的 URL,但是可用的答案不起作用并且已经过时,所以我希望有人知道。

代码如下:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    return true
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
            
    if let incomingURL = userActivity.webpageURL {
        
        let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
            guard error == nil else {
                print(error!.localizedDescription)
                return
            }
            if let dynamicLink = dynamicLink {
                self.handleIncomingDynamicLink(dynamicLink)
            } else {
                print("Something went wrong")
            }
        }
        if linkHandled {
            return true
        } else {
            return false
        }
    }
    return false
}

func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
    guard let url = dynamicLink.url else {
        print("No incoming link")
        return
    }
    print("Link is: \(url)")
    SystemManager.sharedInstance.setDeepLinkTrip(tripKey: deepLinkParser(link: url))
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
    self.window?.rootViewController = initialViewController
}

那么,这些天应用程序冷启动时,如何才能正确打开 link?感谢所有帮助:)

编辑:我已经更新了我的应用程序以使用 SceneDelegate,所有其他答案都建议这样做,但它仍然不起作用。如果 运行 在后台,它仍然会打开动态 links,否则它不会打开它们。看起来没有任何 link 被发送到应用程序。我找不到任何 userActivity、urlContexts 等

这是新的相关代码:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

//I read that this function should be handling the link when it is clicked while the app is dead, but nothing works. Basically this checks that the user is logged in (always is), then looks for a link.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    self.scene(scene, openURLContexts: connectionOptions.urlContexts)
    
    guard let _ = (scene as? UIWindowScene) else { return }

    if Auth.auth().currentUser != nil {
        
        if let userActivity = connectionOptions.userActivities.first {
          self.scene(scene, continue: userActivity)
        } else {
          self.scene(scene, openURLContexts: connectionOptions.urlContexts)
        }
        
        if let userActivity = connectionOptions.userActivities.first {
            if let incomingURL = userActivity.webpageURL {
                _ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
                    guard error == nil else { return }
                    if let dynamicLink = dynamicLink {
                        self.handleIncomingDynamicLink(dynamicLink)
                    }  else {
                       print("Something went wrong")
                       let storyboard = UIStoryboard(name: "Main", bundle: nil)
                       let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
                       self.window?.rootViewController = initialViewController
                   }
                }
            }
        } else {

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
            self.window?.rootViewController = initialViewController
        }
    }
    
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        
    if let url = URLContexts.first?.url {
        _ = DynamicLinks.dynamicLinks().handleUniversalLink(url) { (dynamicLink, error) in
        guard error == nil else { return }
        if let dynamicLink = dynamicLink {
        //your code for handling the dynamic link goes here
            self.handleIncomingDynamicLink(dynamicLink)
            }
        }
    }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    if let incomingURL = userActivity.webpageURL {
        
        _ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
            guard error == nil else {
                print(error!.localizedDescription)
                return
            }
            if let dynamicLink = dynamicLink {
                self.handleIncomingDynamicLink(dynamicLink)
            } else {
                print("Soemthing went wrong")
            }
        }
    }
}

我无法对此进行测试,但它可以帮助处理应用程序关闭时的动态 link。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    if let userActivityDict = launchOptions?[.userActivityDictionary] as? [UIApplication.LaunchOptionsKey : Any],
        let userActivity = userActivityDict[.userActivityType] as? NSUserActivity {

        // execute the dynamic url code here using the userActivity
    }

    return true
}

我知道出了什么问题。该问题与 SceneDelegate 无关 - 我处理 link 的方式被另一个网络调用覆盖。

由于在冷启动时打开应用程序时看不到打印语句,我如何解决此问题的方法是将消息保存到 UserDefaults,然后在应用程序加载后创建弹出警报以读取保存的消息。

最后,我确实需要从 AppDelegate 转移到 SceneDelegate。