Firebase Dynamic Link 无法在应用启动时运行
Firebase Dynamic Link not working on the app launch
我创建了一个 dynamic link
,点击它会打开应用程序。 link 还包含打开特定页面的键。这是我的代码:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
return self.handleFirebaseDynamicLink(userActivity: userActivity)
}
当应用程序在后台 运行 时,此功能有效。但是我还需要动态link来勾选URL
并在第一次启动时打开指定的页面。我能做些什么来解决这个问题?
我在这里找到了一个可接受的答案:
但不幸的是,我无法理解它的含义,也无法对该答案发表评论。
您从 application(_:continue:restorationHandler:)
调用 handleFirebaseDynamicLink
方法 仅 这就是问题所在。
application(_:continue:restorationHandler:)
The app calls this method when it receives the data associated with the user activity. Use the data stored in the NSUserActivity object to re-create the user’s activity. This method is your opportunity to update your app so that it can perform the associated task.
因此只有当您的应用是 Suspended
并且由于 NSUserActivity
再次变为 Active
时才会调用此方法。如果你想在应用程序启动时处理数据,你需要使用另一种方法,当你的应用程序从 Not running
变成 Active
时调用。这是 application(_:didFinishLaunchingWithOptions:)
方法。 Here 您可以阅读有关应用程序生命周期的更多信息。
application(_:didFinishLaunchingWithOptions:)
Use this method to complete your app’s initialization and make any final tweaks. This method is called after state restoration has occurred but before your app’s window and other UI have been presented.
这就是您提供的答案中的内容。您需要做的就是在 AppDelegate
使用此方法处理初始数据。您可以使用以下代码从 launchOptions
中提取 NSUserActivity
。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let activityKey = NSString(string: "UIApplicationLaunchOptionsUserActivityKey")
if let userActivityDict = launchOptions?[.userActivityDictionary] as? [NSObject : AnyObject],
let userActivity = userActivityDict[key] as? NSUserActivity {
return self.handleFirebaseDynamicLink(userActivity: userActivity)
}
return true
}
希望对你有帮助。
我创建了一个 dynamic link
,点击它会打开应用程序。 link 还包含打开特定页面的键。这是我的代码:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
return self.handleFirebaseDynamicLink(userActivity: userActivity)
}
当应用程序在后台 运行 时,此功能有效。但是我还需要动态link来勾选URL
并在第一次启动时打开指定的页面。我能做些什么来解决这个问题?
我在这里找到了一个可接受的答案:
您从 application(_:continue:restorationHandler:)
调用 handleFirebaseDynamicLink
方法 仅 这就是问题所在。
application(_:continue:restorationHandler:)
The app calls this method when it receives the data associated with the user activity. Use the data stored in the NSUserActivity object to re-create the user’s activity. This method is your opportunity to update your app so that it can perform the associated task.
因此只有当您的应用是 Suspended
并且由于 NSUserActivity
再次变为 Active
时才会调用此方法。如果你想在应用程序启动时处理数据,你需要使用另一种方法,当你的应用程序从 Not running
变成 Active
时调用。这是 application(_:didFinishLaunchingWithOptions:)
方法。 Here 您可以阅读有关应用程序生命周期的更多信息。
application(_:didFinishLaunchingWithOptions:)
Use this method to complete your app’s initialization and make any final tweaks. This method is called after state restoration has occurred but before your app’s window and other UI have been presented.
这就是您提供的答案中的内容。您需要做的就是在 AppDelegate
使用此方法处理初始数据。您可以使用以下代码从 launchOptions
中提取 NSUserActivity
。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let activityKey = NSString(string: "UIApplicationLaunchOptionsUserActivityKey")
if let userActivityDict = launchOptions?[.userActivityDictionary] as? [NSObject : AnyObject],
let userActivity = userActivityDict[key] as? NSUserActivity {
return self.handleFirebaseDynamicLink(userActivity: userActivity)
}
return true
}
希望对你有帮助。