我已经成功地在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()
}
有谁能告诉我为什么第一次上述方法没有调用吗?