简体   繁体   English

我如何知道应用程序是否通过AppDelegate的didFinishLaunchingWithOptions上的Firebase-Deeplink(动态链接)启动

[英]How do I know if app was launched via Firebase-Deeplink (Dynamic Links) at AppDelegate's didFinishLaunchingWithOptions

We are adding Firebase-Deeplinks to our IOS-project, so that the app can be started via deeplink. 我们正在将Firebase-Deeplinks添加到我们的IOS项目中,以便可以通过Deeplink启动该应用程序。

The deeplink-feature itself work fine so far, and so does the default app launch routine. 到目前为止,deeplink功能本身可以正常工作,默认的应用启动例程也可以正常工作。 But making both startRoutines work side by side gives me some headache. 但是让两个startRoutines一起工作会使我有些头疼。

What I am trying to achieve get's obvious looking at this code snippet. 我正在尝试实现的目标很明显地看了一下这段代码。

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

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

If there is a deeplink one of these appDelegate-functions is called: 如果存在深层链接,则将这些appDelegate函数之一称为:

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

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

So how do I know at application(_:didFinishLaunchingWithOptions:) if I can call defaultAppLaunch() ? 那么我如何知道application(_:didFinishLaunchingWithOptions :)是否可以调用defaultAppLaunch()

I know there is the launchOptions -Argument in but in my case it is always nil , at least when running the app via XCode. 我知道其中包含launchOptions -Argument,但就我而言,它始终为nil ,至少在通过XCode运行应用程序时如此。 And also the Firebase-Documentation says nothing about launchOptions to be set by Firebase-Deeplinks. 而且,Firebase文档对由Firebase-Deeplinks设置的launchOptions没有任何说明。

Help is highly appreciated. 非常感谢您的帮助。

I'm referencing the Firebase docs in handling dynamic links for iOS: Firebase docs for receiving dynamic links 我在处理iOS的动态链接时引用了Firebase文档: 用于接收动态链接的Firebase文档

Next, in the application:continueUserActivity:restorationHandler: method, handle links received as Universal Links when the app is already installed (on iOS 9 and newer): 接下来,在application:continueUserActivity:restorationHandler:方法中,处理已安装应用程序(在iOS 9及更高版本上)作为通用链接接收的链接:

func application(_ application: UIApplication, continue userActivity: NSUserActivity,
                 restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
  let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
    // ...
  }

  return handled
}

Finally, in the application:openURL:sourceApplication:annotation: (iOS 8 and older) and application:openURL:options: (iOS 9 and up) methods, handle links received through your app's custom URL scheme. 最后,在application:openURL:sourceApplication:annotation:(iOS 8及更高版本)和application:openURL:options:(iOS 9及更高版本)方法中,处理通过应用程序的自定义URL方案接收的链接。 These methods are called when your app receives a link on iOS 8 and older, and when your app is opened for the first time after installation on any version of iOS. 当您的应用在iOS 8和更早版本上收到链接时,以及在任何版本的iOS上安装后首次打开应用时,都会调用这些方法。

@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
  return application(app, open: url,
                     sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                     annotation: "")
}

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
}

But you did mention that the app is currently only being run on Xcode (and I'm guessing iOS Simulator, maybe you can try it on a test device too!) 但是您确实提到了该应用程序当前仅在Xcode上运行(我猜是iOS模拟器,也许您也可以在测试设备上尝试它!)

TL;DR TL; DR

You can't know that your app was opened using deeplinks through App Delegate DidFinishLaunching. 您不知道您的应用是通过App Delegate DidFinishLaunching使用深层链接打开的。


Explaination: 说明:

App delegate did finish launch is always called, regardless if app was opened normally or via deeplinks. 无论应用程序是正常打开还是通过深度链接打开, 始终会调用完成的启动的应用程序委托。 so you can't know through app delegate 因此您无法通过应用委托了解

Instead, you can know that app was opened through deeplinks if the following delegate function is called. 相反,如果调用以下委托函数,则可以知道该应用是通过深层链接打开的。

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
}

and you should handle the deeplinks functionality in the same function 并且您应该在同一函数中处理Deeplinks功能

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序通过远程推送启动didFinishLaunchingWithOptions时延迟openURL - Delaying openURL when app launched via remote push through didFinishLaunchingWithOptions 如何检测我的应用程序是否已在didFinishLaunchingWithOptions方法中进行后台提取? - How can I detect my app was launched for background fetch in didFinishLaunchingWithOptions method? 由于位置更改而启动应用程序时,如何实现didFinishLaunchingWithOptions方法? - How to implement didFinishLaunchingWithOptions method when the app is launched as a result of location change? 未启动应用程序时 Firebase 动态链接不起作用 - Firebase Dynamic Links not working when the application is not launched AppDelegate 的用途是什么,我如何知道何时使用它? - What is the AppDelegate for and how do I know when to use it? 我如何在AppDelegate中知道UIViewController过渡到另一个UIViewController? - How do I know in AppDelegate when UIViewController transit to another UIViewController? 如何在didFinishLaunchingWithOptions AppDelegate中使用框架函数? - How to use framework func in didFinishLaunchingWithOptions AppDelegate? 如何知道是否由于用户启动应用程序或系统而调用didFinishLaunchingWithOptions - How to know whether didFinishLaunchingWithOptions is called because of user launching the app or the system 如何在 AppDelegate 中初始化 Core Data 的持久化容器并在整个应用程序中使用它? - How do I initialize the Core Data's persistent container in AppDelegate and use it throughout the app? 如何使用 react-native-firebase 从 Firebase 动态链接接收查询参数? - How do I receive query parameters from Firebase Dynamic Links using react-native-firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM