7

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

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

4

4 回答 4

12

你几乎拥有它:

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. (虽然如果发生的事情是用户点击了一个链接来启动我们,我希望在 .) 中找到该 URL connectionOptions.urlContexts。)

于 2019-10-28T01:25:27.933 回答
4

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

于 2019-10-28T01:11:33.067 回答
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中,因此您必须使用循环搜索它。

于 2020-10-14T11:35:44.093 回答
1

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)'")
}

我已经验证这适用于我的应用程序。有关更多详细信息,请参阅此 Github 线程

于 2021-12-30T19:54:06.447 回答