gpt4 book ai didi

iphone - 方法 'scene(_:openURLContexts:)'未被调用

转载 作者:行者123 更新时间:2023-12-03 18:33:06 26 4
gpt4 key购买 nike

在 info.plist 文件中,我成功配置了 URL IdentifierURL 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()
}

谁能告诉我为什么上面的方法第一次没有调用?

最佳答案

也许这会对您的情况有所帮助。


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

}

更新于2021年6月17日

如果您想使用 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))")

}

谢谢

关于iphone - 方法 'scene(_:openURLContexts:)'未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58973143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com