首页 > 解决方案 > 我如何知道应用程序是否通过 AppDelegate 的 didFinishLaunchingWithOptions 的 Firebase-Deeplink(动态链接)启动

问题描述

我们正在将 Firebase-Deeplinks 添加到我们的 IOS 项目中,以便可以通过深度链接启动应用程序。

到目前为止,深度链接功能本身运行良好,默认应用程序启动例程也是如此。但是让两个 startRoutines 并排工作让我有些头疼。

我想要实现的目标很明显,看看这个代码片段。

func application(_:didFinishLaunchingWithOptions:) {
   FirebaseApp.configure()

   if "deeplink" {
      return true 
   }
   defaultAppLaunch() // no deeplink
   return true 
}

如果存在深层链接,则调用这些 appDelegate-functions 之一:

func application(:continueUserActivity:restorationHandler:) {
    handleDeeplink()
    return true
}

func application(:openURL:options:) {
    handleDeeplink()
    return true  
}

那么我怎么知道 application(_:didFinishLaunchingWithOptions:) 是否可以调用defaultAppLaunch()

我知道有launchOptions -Argument 但在我的情况下它总是nil,至少在通过 XCode 运行应用程序时是这样。Firebase-Documentation 也没有说明由 Firebase-Deeplinks 设置的 launchOptions。

高度赞赏帮助。

标签: iosswiftfirebaseappdelegatedeeplink

解决方案


TL;博士

您无法知道您的应用是通过 App Delegate DidFinishLaunching 使用深层链接打开的。


解释:

无论应用程序是正常打开还是通过深层链接打开,始终会调用应用程序委托确实完成启动。所以你不能通过应用委托知道

相反,如果调用以下委托函数,您可以知道应用程序是通过深层链接打开的。

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
  if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
    // Handle the deep link. For example, show the deep-linked content or
    // apply a promotional offer to the user's account.
    // ...
    return true
  }
  return false
}

你应该在同一个函数中处理深层链接功能


推荐阅读