【发布时间】:2020-03-17 06:43:57
【问题描述】:
在 info.plist 文件中我成功配置了URL Identifier 和URL Scheme。我还可以使用自定义 URL 打开应用程序。问题是当应用第一次启动时,方法
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)
不会被调用。
我有一些基于上述方法的依赖功能。因此,当应用首次启动时,我无法在我的应用中看到任何内容。
我还在方法中添加了代码
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
let url = connectionOptions.urlContexts.first?.url
}
但我在这里得到的 url 为 nil。
但是,如果我的应用程序处于后台模式并且我点击了 URL,那么上述方法调用成功并且相关功能可以正常工作。以下是我在sceneDelegate 中的scene(_:openURLContexts:) 方法的代码。
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()
}
谁能告诉我为什么上面的方法第一次没有调用?
【问题讨论】:
-
这个问题的当前状态是什么?其中一个答案是否解决了您的问题?如果是,请接受其中之一。您需要更多帮助吗?如果是,您能否解释所提供的解决方案对您的问题做了什么以及仍然缺少什么?我很乐意帮助你解决这个问题
标签: iphone ios13 swift5 custom-scheme-url