方法'scene(_:openURLContexts :)'未调用


9

在Info.plist文件我配置URL IdentifierURL Scheme成功。我也可以使用自定义网址打开应用。问题是应用首次启动时的方法

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) does not call.

我有一些基于上述方法的依赖功能。因此,当应用首次启动时,我无法在应用中看到任何内容。

我也在方法中添加了代码

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let _ = (scene as? UIWindowScene) else { return }

        let url = connectionOptions.urlContexts.first?.url

    }

但我在这里得到的网址为nil。

但是,如果我的应用程序处于后台模式并且点击了URL,则上述方法将成功调用并且相关功能可以正常工作。以下是我在中的scene(_:openURLContexts:)方法代码sceneDelegate

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>){
        let url = URLContexts.first?.url
        let urlString: String = url!.absoluteString

        if let urlComponents = URLComponents(string: urlString),let queryItems = urlComponents.queryItems {
            queryParams = queryItems
        } else {
            print("invalid url")
        }

        guard let windowScene = (scene as? UIWindowScene) else { return }


        self.window = UIWindow(windowScene: windowScene)
        //self.window =  UIWindow(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let rootVC = storyboard.instantiateViewController(identifier: "LocationViewIdentifier") as? UIViewController else {
            print("ViewController not found")
            return
        }
        let rootNC = UINavigationController(rootViewController: rootVC)
        self.window?.rootViewController = rootNC
        self.window?.makeKeyAndVisible()
    }

谁能告诉我为什么以上方法第一次不调用?


这个问题的当前状态是什么?答案之一解决了您的问题吗?如果是,请接受其中之一。您需要更多帮助吗?如果是,您能解释一下所提供的解决方案对您的问题做了什么,以及仍然缺少什么?我很高兴为您提供帮助
Muli

Answers:


2

也许这会帮助您解决问题。

func scene(_ scene:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.ConnectionOptions){

    让urlinfo = connectionOptions.urlContexts
    让url = urlinfo.first?.url为!网址

}

func scene(_ scene:UIScene,openURLContexts URLContexts:Set){

    让url = URLContexts.first!.url作为NSURL

}


2

我遇到了同样的问题,并且scene(_ scene:, continue userActivity:)仅在打开应用程序时才调用该方法。

当我检查connectionOptions传递给该方法的方法时,scene(_ scene:, willConnectTo session, options connectionOptions:)它具有预期URL的用户活动。因此,我最终手动调用了第一个方法:

func scene(_ scene: UIScene,
           willConnectTo session: UISceneSession,
           options connectionOptions: UIScene.ConnectionOptions) {

    if let userActivity = connectionOptions.userActivities.first {
        self.scene(scene, continue: userActivity)
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.