gpt4 book ai didi

ios - `scene(_ scene: UIScene, continue userActivity: NSUserActivity)` 用户点击通用链接后启动应用时不被调用

转载 作者:行者123 更新时间:2023-12-01 15:38:56 51 4
gpt4 key购买 nike

方法scene(_ scene: UIScene, continue userActivity: NSUserActivity)用户单击通用链接后启动应用程序时不会调用。

当用户单击通用链接后再次打开已启动的应用程序时,它工作正常。示例代码:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
let path = components.path else {
return
}
let params = components.queryItems ?? [URLQueryItem]()

print("path = \(path)")
print("params = \(params)")
}

我尝试使用 application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration ,但是当用户点击链接时它永远不会被调用:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
if let scene = connectingSceneSession.scene, let userActivity = scene.userActivity {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
if let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
let path = components.path {
let params = components.queryItems ?? [URLQueryItem]()

print("path = \(path)")
print("params = \(params)")
}
}
}
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

我尝试使用 scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) :
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = scene.userActivity {
self.scene(scene, continue: userActivity)
}
}

我还尝试了以下方法:
func sceneDidBecomeActive(_ scene: UIScene) {
if let userActivity = scene.userActivity {
self.scene(scene, continue: userActivity)
}
}

func sceneWillEnterForeground(_ scene: UIScene) {
if let userActivity = scene.userActivity {
self.scene(scene, continue: userActivity)
}
}

但是 scene.userActivity那里总是为零,我无法得到 userActivity.webpageURL .

我们如何识别链接被点击并且应用程序被启动(不仅仅是打开)?

最佳答案

你几乎拥有它:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = scene.userActivity { // <-- not quite
self.scene(scene, continue: userActivity)
}
}
它不在场景中;它在 connectionOptions .查看 connectionOptions.userActivities . (虽然如果发生的事情是用户点击了一个链接来启动我们,我希望在 connectionOptions.urlContexts 中找到 URL。)

关于ios - `scene(_ scene: UIScene, continue userActivity: NSUserActivity)` 用户点击通用链接后启动应用时不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58243884/

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