不会调用方法'scene(_:openURLContexts :)' [英] Method 'scene(_:openURLContexts:)' is not called
问题描述
在info.plist文件中,我成功配置了URL Identifier
和URL Scheme
.我也可以使用自定义网址打开应用.问题是当应用首次启动该方法时
In info.plist file I configured URL Identifier
and URL Scheme
successfully. Also I am able to open app using custom URL. The problem is when app launches first time the method
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)
不打电话.
我有一些基于上述方法的依赖功能.因此,当应用程序首次启动时,我无法在应用程序中看到任何内容.
I have some dependent functionality based on above method. So when app launches first time I am not able to see anything in my app.
我也在方法中添加了代码
Also I added code in 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
}
但是我在这里得到的网址为nil.
but I get url as nil here.
但是,如果我的应用程序处于后台模式并且我点击了URL,则上述方法调用成功并且相关功能正常运行.以下是我在sceneDelegate
中scene(_:openURLContexts:)
方法上的代码.
However if my app is in background mode and I hit URL then above method calls successfully and dependent functionality working fine. Following is my code on scene(_:openURLContexts:)
method in 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) {
let url = URLContexts.first!.url as NSURL
}
这篇关于不会调用方法'scene(_:openURLContexts :)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!