简体   繁体   English

未调用方法“场景(_:openURLContexts :)”

[英]Method 'scene(_:openURLContexts:)' is not called

In the info.plist file I configured URL Identifier and URL Scheme successfully.在 info.plist 文件中,我成功配置URL IdentifierURL Scheme Also I am able to open the app using the custom URL.我还可以使用自定义 URL 打开应用程序。 The problem is when the app launches for the first time, the method问题是当应用程序第一次启动时,方法

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) 

does not get called.不会被调用。

I have some dependent functionality based on the above method.我有一些基于上述方法的依赖功能。 So when the app launches for the first time, I am not able to see anything in my app.因此,当应用程序第一次启动时,我无法在我的应用程序中看到任何内容。

Also I added code in the method我还在方法中添加了代码

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        guard let _ = (scene as? UIWindowScene) else { return }
        
        let url = connectionOptions.urlContexts.first?.url
        
    }

but I get url as nil here.但我在这里得到 url 为零。

However if my app is in background mode and I hit URL then above method calls successfully and dependent functionality working fine.但是,如果我的应用程序处于后台模式并且我点击 URL 则上述方法调用成功并且相关功能正常工作。 Following is my code on scene(_:openURLContexts:) method in sceneDelegate .以下是我在 sceneDelegate 中的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()
    }

Can anyone tell me why first time above method does not call?谁能告诉我为什么上面的方法第一次没有调用?

Maybe this will help your situation.也许这会对您的情况有所帮助。


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
       
    let urlinfo = connectionOptions.urlContexts
    let url = urlinfo.first?.url as! NSURL
  
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext*h>) {
       
    let url = URLContexts.first!.url as NSURL

}

Update at 17.6.2021更新于 17.6.2021

If you wish to use Deeplink and Universal Link both this is my method.如果您希望同时使用 Deeplink 和 Universal Link,这是我的方法。

    //Deeplink or Universial Link Open when app is start.
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
                
        // Universial link Open
        if let userActivity = connectionOptions.userActivities.first,
           userActivity.activityType == NSUserActivityTypeBrowsingWeb,
           let urlinfo = userActivity.webpageURL{
            
            print ("Universial Link Open at SceneDelegate on App Start ::::::: \(urlinfo)")
            
        }
        
        //deeplink Open
        if connectionOptions.urlContexts.first?.url != nil {
          let urlinfo = connectionOptions.urlContexts.first?.url
            
            print ("Deeplink Open at SceneDelegate on App Start ::::::: \(String(describing: urlinfo))")

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

    // Universial link Open when app is onPause
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb,
            let urlinfo = userActivity.webpageURL{
            
            print ("Universial Link Open at SceneDelegate on App Pause  ::::::: \(urlinfo)")
            
        }
    }
    
    // Deeplink Open when app in onPause
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        
        let urlinfo = URLContexts.first?.url
        print ("Deeplink Open on SceneDelegate at App Pause :::::::: \(String(describing: urlinfo))")

    }

Thanks谢谢

I had the same problem, and the method scene(_ scene:, continue userActivity:) was only called when the app is open.我遇到了同样的问题,只有在应用打开时才调用方法scene(_ scene:, continue userActivity:)

When I checked the connectionOptions passed to the method scene(_ scene:, willConnectTo session, options connectionOptions:) it had a user activity with the expected URL.当我检查传递给方法场景的connectionOptions scene(_ scene:, willConnectTo session, options connectionOptions:)它有一个预期的用户活动URL。 So, I ended up calling the first method manually:所以,我最终手动调用了第一个方法:

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

    if let userActivity = connectionOptions.userActivities.first {
        self.scene(scene, continue: userActivity)
    }
}

My situation was that the openURL extra function provided in the SceneDelegate was not being called after returning from Facebook Authentication.我的情况是从 Facebook 身份验证返回后,未调用 SceneDelegate 中提供的 openURL 额外 function。

The solution was using解决方案是使用

.onOpenURL { (url) in
                ApplicationDelegate.shared.application(
                    UIApplication.shared,
                    open: url,
                    sourceApplication: nil,
                    annotation: [UIApplication.OpenURLOptionsKey.annotation]
                )
            }

To call the function in my extra application delegate, which then worked properly.在我的额外应用程序委托中调用 function,然后它可以正常工作。

I think the Apple docs explain it most clearly, ie,我认为苹果文档解释得最清楚,即

If your app has opted into Scenes, and your app is not running, the system delivers the URL to the scene(_:willConnectTo:options:) delegate method after launch, and to scene(_:openURLContexts:) when your app opens a URL while running or suspended in memory.如果您的应用已选择加入场景,并且您的应用未运行,则系统会在启动后将 URL 传递给scene(_:openURLContexts:) scene(_:willConnectTo:options:)委托方法,并在您的应用打开一个URL 在 memory 中运行或暂停时。

Hence, it would only call your scene(_:openURLContexts:) (ie, the one with this signature: func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) in the question) when your app opens a URL while running or suspended in memory only.因此,它只会在您的应用程序打开 URL 时调用您的scene(_:openURLContexts:) (即问题中具有此签名的场景: func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) ,而仅在 memory 中运行或暂停。

Instead, when the app is launched the first time, it calls scene(_:willConnectTo:options:) .相反,当应用程序第一次启动时,它会调用scene(_:willConnectTo:options:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM