首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“场景(_场景: UIScene,继续userActivity: NSUserActivity)”在用户单击通用链接后启动应用程序时不会被调用

“场景(_场景: UIScene,继续userActivity: NSUserActivity)”在用户单击通用链接后启动应用程序时不会被调用
EN

Stack Overflow用户
提问于 2019-10-04 22:43:13
回答 4查看 4K关注 0票数 14

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

当已经启动的应用程序在用户单击通用链接后再次打开时,它工作得很好。示例代码:

代码语言:javascript
复制
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,但是当用户单击链接时,它就不会被调用:

代码语言:javascript
复制
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)

代码语言:javascript
复制
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let userActivity = scene.userActivity {
        self.scene(scene, continue: userActivity)
    }
}

我还尝试了以下方法:

代码语言:javascript
复制
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

我们如何才能识别出链接是被点击并启动的(而不仅仅是打开)?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-10-28 01:25:27

你差点就有了:

代码语言:javascript
复制
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。)

票数 13
EN

Stack Overflow用户

发布于 2021-12-30 19:54:06

马特接受的答案是,当应用程序尚未打开时,可以启动通用链接。

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

代码语言:javascript
复制
// 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线程

票数 7
EN

Stack Overflow用户

发布于 2020-10-14 11:35:44

这对我起了作用:

代码语言:javascript
复制
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中是“隐藏的”,所以您必须使用循环搜索它。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58243884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档