问题
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>) does not call.
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
}
but I get url as nil here.
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?
回答1:
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 }
来源:https://stackoverflow.com/questions/58973143/opening-app-using-custom-url-scheme-first-time-scene-openurlcontexts-does-n