在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()
}谁能告诉我为什么上面的方法第一次不调用?
发布于 2019-12-06 13:47:26
也许这会对你的情况有所帮助。
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
}在17.6.2021更新
如果你想同时使用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))")
}谢谢
发布于 2019-12-17 17:07:24
我也遇到了同样的问题,只有在应用程序打开时才会调用scene(_ scene:, continue userActivity:)方法。
当我检查传递给scene(_ scene:, willConnectTo session, options connectionOptions:)方法的connectionOptions时,它有一个包含预期URL的用户活动。因此,我最终手动调用了第一个方法:
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = connectionOptions.userActivities.first {
self.scene(scene, continue: userActivity)
}
}发布于 2021-11-24 16:16:14
我认为the Apple docs解释得最清楚,也就是,
如果你的应用程序已经选择进入场景,并且你的应用程序没有运行,系统会在启动后将URL传递给
scene(_:willConnectTo:options:)委托方法,当应用程序在运行或挂起时打开一个URL时,系统会传递给scene(_:openURLContexts:)。
因此,当你的应用程序在运行或挂起内存时打开一个scene(_:openURLContexts:)时,它只会调用你的URL (也就是问题中签名为func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)的那个)。
相反,当应用程序第一次启动时,它会调用scene(_:willConnectTo:options:)。
https://stackoverflow.com/questions/58973143
复制相似问题