gpt4 book ai didi

swift - 应用程序关闭时动态链接不起作用,仅在后台运行时

转载 作者:行者123 更新时间:2023-12-03 09:25:55 33 4
gpt4 key购买 nike

我一直在努力在我的应用程序中包含动态链接。我已经对其进行了设置,以便可以正确生成和接收链接,但它们仅在应用程序打开并在后台运行时才有效。如果应用程序完全关闭,则链接只会打开应用程序。看起来它没有调用 AppDelegate 中的 continueUserActivity 方法,即使我在 didFinishLaunchingWithOptions 方法中返回 TRUE。我已经读到我应该在 didFinishLaunchingWithOptions 方法中获取传入的 URL,但是可用的答案不起作用并且已经过时,所以我希望有人知道。
这是代码:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

if let incomingURL = userActivity.webpageURL {

let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
if let dynamicLink = dynamicLink {
self.handleIncomingDynamicLink(dynamicLink)
} else {
print("Something went wrong")
}
}
if linkHandled {
return true
} else {
return false
}
}
return false
}

func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
guard let url = dynamicLink.url else {
print("No incoming link")
return
}
print("Link is: \(url)")
SystemManager.sharedInstance.setDeepLinkTrip(tripKey: deepLinkParser(link: url))

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
self.window?.rootViewController = initialViewController
}
那么如何才能在应用程序冷启动时正确打开链接呢?感谢所有帮助:)
编辑:我已经更新了我的应用程序以使用 SceneDelegate,所有其他答案都建议使用它,但它仍然不起作用。如果在后台运行,它仍然会打开动态链接,否则不会打开它们。看起来没有任何链接被发送到应用程序。我找不到任何 userActivity、urlContexts 等。
继承人新的相关代码:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

//I read that this function should be handling the link when it is clicked while the app is dead, but nothing works. Basically this checks that the user is logged in (always is), then looks for a link.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

self.scene(scene, openURLContexts: connectionOptions.urlContexts)

guard let _ = (scene as? UIWindowScene) else { return }

if Auth.auth().currentUser != nil {

if let userActivity = connectionOptions.userActivities.first {
self.scene(scene, continue: userActivity)
} else {
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
}

if let userActivity = connectionOptions.userActivities.first {
if let incomingURL = userActivity.webpageURL {
_ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else { return }
if let dynamicLink = dynamicLink {
self.handleIncomingDynamicLink(dynamicLink)
} else {
print("Something went wrong")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
self.window?.rootViewController = initialViewController
}
}
}
} else {

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
self.window?.rootViewController = initialViewController
}
}

}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {

if let url = URLContexts.first?.url {
_ = DynamicLinks.dynamicLinks().handleUniversalLink(url) { (dynamicLink, error) in
guard error == nil else { return }
if let dynamicLink = dynamicLink {
//your code for handling the dynamic link goes here
self.handleIncomingDynamicLink(dynamicLink)
}
}
}
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if let incomingURL = userActivity.webpageURL {

_ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
if let dynamicLink = dynamicLink {
self.handleIncomingDynamicLink(dynamicLink)
} else {
print("Soemthing went wrong")
}
}
}
}

最佳答案

我想出了什么问题。该问题与 SceneDelegate 无关 - 我处理链接的方式被另一个网络调用覆盖。
我是如何解决这个问题的,因为在从冷启动打开应用程序时看不到打印语句,是通过将消息保存到 UserDefaults,然后在应用程序加载后创建一个弹出警报以读取保存的消息。
最后,我确实需要通过 AppDelegate 移动到 SceneDelegate。

关于swift - 应用程序关闭时动态链接不起作用,仅在后台运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62683639/

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