Dynamic Links Firebase function not being called at all Xcode 12?

痞子三分冷 提交于 2021-01-03 06:21:52

问题


So I have a dynamic link that is working in that it opens up the app when I click on it but the handling of the dynamic link doesn't happen. This is because application function seen below is never entered and I'm not sure why...

func handleIncomingDynamicLink(_ dynamicLink: DynamicLink){
    guard let url = dynamicLink.url else {
        print("That's weird. My dynamic link object has no url")
        return
    }
    print("Your incoming link parameter is \(url.absoluteString)")
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if let incomingURL = userActivity.webpageURL {
        print("Incoming URL is \(incomingURL)")
        let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
            guard error == nil else{
                print("Found an error! \(error!.localizedDescription)")
                return
            }
            if let dynamicLink = dynamicLink {
                self.handleIncomingDynamicLink(dynamicLink)
            }
        }
        if linkHandled {
            return true
        } else {
            return false
        }
    }
    return false
}

Any help would be greatly appreciated--is it possible this is a problem with Firebase itself or no?


回答1:


SwiftUI 1.0 / SceneDelegate

You do the same but in your SceneDelegate, following callback:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    if let incomingURL = userActivity.webpageURL {
        print("Incoming URL is \(incomingURL)")
        let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
            guard error == nil else{
                print("Found an error! \(error!.localizedDescription)")
                return
            }
            if let dynamicLink = dynamicLink {
                self.handleIncomingDynamicLink(dynamicLink)
            }
        }
}

SwiftUI 2.0 / WindowGroup

With SwiftUI 2.0 life-cycle you need to use instead .onContinueUserActivity, like

WindowGroup {
    ContentView()
     .onContinueUserActivity(<your_activity_type_here>) { userActivity in
         if let incomingURL = userActivity.webpageURL {
              print("Incoming URL is \(incomingURL)")
              let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
                    guard error == nil else{
                         print("Found an error! \(error!.localizedDescription)")
                         return
                    }
                    if let dynamicLink = dynamicLink {
                         self.handleIncomingDynamicLink(dynamicLink)
                    }
              }
        }
    }
}



回答2:


SwiftUI 2.0 / @main App For the new SwiftUI 2.0 you can make an app only using SwiftUI. This means you don't use AppDelegate or SceneDelegate. You then have to use .onOpenURL to get any action called when you click on your link. I am usling this for Firebase Dynamic Links.

WindowGroup {
  ContentView()
    .onOpenURL { url in
      // handle the URL that must be opened
      let incomingURL = url
      print("Incoming URL is: \(incomingURL)")
      let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
          guard error == nil else {
             print("Error found!: \(error!.localizedDescription)")
                return
          }
          if let dynamicLink = dynamicLink {
             self.handleDynamicLink(dynamicLink)
          }
       }
       if linkHandled{
          print("Link Handled")
          return
       }else{
          print("NO linkHandled")
          return
       }
    }
}


来源:https://stackoverflow.com/questions/63893945/dynamic-links-firebase-function-not-being-called-at-all-xcode-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!