【问题标题】:`scene(_ scene: UIScene, continue userActivity: NSUserActivity)` doesn't get called when the app is launched after the user clicks on a universal link`scene(_scene: UIScene, continue userActivity: NSUserActivity)` 在用户点击通用链接后启动应用程序时不会被调用
【发布时间】:2020-02-03 05:36:56
【问题描述】:

在用户单击通用链接后启动应用程序时,不会调用方法 scene(_ scene: UIScene, continue userActivity: NSUserActivity)

当用户点击通用链接后,已经启动的应用再次打开时,它工作正常。示例代码:

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,但是当用户点击链接时它永远不会被调用:

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

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

【问题讨论】:

标签: ios ios13 ios-universal-links uiscene uiscenedelegate


【解决方案1】:

Apple 回应确认 iOS 13 中存在该问题。

【讨论】:

  • 你有苹果确认的问题的链接吗?接受的答案对我不起作用,当应用程序关闭并单击动态链接时,应用程序打开但 url 为 nil。
  • 现在解决了吗?
【解决方案2】:

你差点就吃完了:

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

它不在场景中;它在connectionOptions 中。查看connectionOptions.userActivities。 (虽然如果发生的事情是用户点击了一个链接来启动我们,我希望在connectionOptions.urlContexts中找到该URL。)

【讨论】:

  • 仍然不适合我。当应用程序关闭时。 (从最近的应用中滑动)动态链接打开应用,但 url 为 nil
  • 这个答案似乎已经过时了。关于迭代 connectionOptions.userActivities 的另一个答案对我有用。
  • @Mick 看看我实际写的内容,在我看来这正是我所说的:“看看connectionOptions.userActivities”。迭代就是你的样子。我不会详细介绍,因为这一切似乎都很明显。 :)
  • @matt 对不起!我只是在没有阅读您的笔记的情况下尝试了代码块。 :) 你可以删除它...
【解决方案3】:

这对我有用:

func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        for userActivity in connectionOptions.userActivities {
            if let url = userActivity.webpageURL { //ADD WHATEVER CONDITION YOU NEED
                //DO WHAT YOU NEED HERE
                break
            }
        }
    }

基本上问题是通用链接“隐藏”在connectionOptions中,因此您必须使用循环搜索它。

【讨论】:

    【解决方案4】:

    Matt 接受的答案适用于在应用尚未打开时启动通用链接。

    如果您还想在应用打开时处理通用链接,则需要以下两个函数:

    
    // SceneDelegate.swift
    // This function is called when your app launches.
    // Check to see if our app was launched with a universal link. 
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
       // See if our app is being launched via universal link.
       for userActivity in connectionOptions.userActivities {
         if let universalLink = userActivity.webpageURL {
             // Do whatever you want with the universal link here.
             // NOTE: if you're navigating a web view, know that the web view will not be initialized here yet. 
             // To navigate a web view, store the URL in a variable and navigate to it once the web view is initialized.
         }
      }
    }
    
    // SceneDelegate.swift
    // This function is called when your app is already running and a universal link to your app is clicked.
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        // Ensure we're trying to launch a link.
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
            let universalLink = userActivity.webpageURL else {
            return
        }
    
        // Handle the universal link here.
        // If you're navigating a web view, here's how I do it:
        //MyApp.webView.evaluateJavaScript("location.href = '\(universalLink)'")
    }
    
    

    我已验证这适用于我的应用。详情请见this Github thread

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2019-08-30
      • 2020-07-29
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多