本文介绍了用户单击通用链接后启动应用程序时,不会调用scene(_ scene:UIScene,继续userActivity:NSUserActivity)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法scene(_ scene: UIScene, continue userActivity: NSUserActivity)不会被调用.

当用户单击通用链接后再次启动已启动的应用程序时,它运行良好.示例代码:

It works fine when already launched app opens again after the user clicks on the universal link. The sample code:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let incomingURL = userActivity.webpageURL,
        let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
        let path = components.path else {
            return
    }
    let params = components.queryItems ?? [URLQueryItem]()

    print("path = \(path)")
    print("params = \(params)")
}

我尝试使用application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration,但是当用户单击链接时,它从未被调用:

I tried to use application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration, but it never gets called when the user clicks on the link:

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    if let scene = connectingSceneSession.scene, let userActivity = scene.userActivity {
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
            if let incomingURL = userActivity.webpageURL,
                let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
                let path = components.path {
                let params = components.queryItems ?? [URLQueryItem]()

                print("path = \(path)")
                print("params = \(params)")
            }
        }
    }
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

我尝试使用scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions):

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let userActivity = scene.userActivity {
        self.scene(scene, continue: userActivity)
    }
}

我还尝试了以下方法:

func sceneDidBecomeActive(_ scene: UIScene) {
    if let userActivity = scene.userActivity {
        self.scene(scene, continue: userActivity)
    }
}

func sceneWillEnterForeground(_ scene: UIScene) {
    if let userActivity = scene.userActivity {
        self.scene(scene, continue: userActivity)
    }
}

但是scene.userActivity在那里总是零,我无法获得userActivity.webpageURL.

But scene.userActivity is always nil there and I can't get userActivity.webpageURL.

我们如何识别链接已被单击并启动了应用程序(不仅仅是打开了)?

How can we recognize that the link was clicked and the app was launched (not just opened)?

推荐答案

您几乎拥有它:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let userActivity = scene.userActivity { // <-- not quite
        self.scene(scene, continue: userActivity)
    }
}

它不在现场;在options中.查看options.userActivities. (尽管发生的事情是用户单击了 link 来启动我们,但我希望能在options.urlContexts中找到该URL.)

It's not in the scene; it's in the options. Look in the options.userActivities. (Though if what has happened is that the user clicked a link to launch us, I would expect to find the URL in the options.urlContexts.)

这篇关于用户单击通用链接后启动应用程序时,不会调用scene(_ scene:UIScene,继续userActivity:NSUserActivity)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:30