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

问题描述 投票:0回答:4

我一直在努力在我的应用程序中包含动态链接。我已经对其进行了设置,以便可以正确生成并正确接收链接,但它们仅在应用程序打开并在后台运行时才有效。如果应用程序完全关闭,该链接将仅打开该应用程序。即使我在 didFinishLaunchingWithOptions 方法中返回 TRUE,它看起来也没有调用 AppDelegate 中的 continueUserActivity 方法。我读过,我应该在 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")
            }
        }
    }
}
swift firebase deep-linking firebase-dynamic-links uiscenedelegate
4个回答
3
投票

我知道出了什么问题了。该问题与 SceneDelegate 无关 - 我处理链接的方式被另一个网络调用覆盖。

由于从冷启动打开应用程序时无法看到打印语句,因此我解决此问题的方法是将消息保存到 UserDefaults,然后创建弹出警报以在应用程序加载后读取保存的消息。

最终,我确实需要迁移到 SceneDelegate 而不是 AppDelegate。


1
投票

适用于:

  • 应用程序位于后台或前台

  • 应用程序已安装但关闭

  • app未安装,点击Firebase Dynamiclink,从AppleStore下载,打开App

    import UIKit
    import Firebase
    import FirebaseDynamicLinks
    
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
      //Call when app is close
      func scene(_ scene: UIScene,
                 willConnectTo session: UISceneSession,
                 options connectionOptions: UIScene.ConnectionOptions) {
    
          if let userActivity = connectionOptions.userActivities.first {
              if let incomingURL = userActivity.webpageURL {
                  handleFirebaseDynamicLink(url: incomingURL)
              }
          }
      }
    
      // Call for a launched app after download from AppleStore
      func scene(_ scene: UIScene,
                 openURLContexts URLContexts: Set<UIOpenURLContext>) {
          if let schemeURL = URLContexts.first?.url {
              handleFirebaseDynamicLinksFromScheme(schemeURL)
          }
      }
    
      // Call while runing
      func scene(_ scene: UIScene,
                 continue userActivity: NSUserActivity) {
          guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
                let url = userActivity.webpageURL else {
              return
          }
          handleFirebaseDynamicLink(url: url)
      }
    
    }
    
    // MARK: Private
    
    extension SceneDelegate {
    
      private func handleFirebaseDynamicLink(url: URL) {
          DynamicLinks.dynamicLinks()
              .handleUniversalLink(url) { dynamiclink, error in
                  if let error = error {
                      return
                  }
                  guard let dynamiclink = dynamiclink else {
                      return
                  }
                  guard let url = dynamiclink.url else {
                      return
                  }
                  self.handle(url: url)
              }
      }
    
      private func handleFirebaseDynamicLinksFromScheme(_ url: URL) {
          guard let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url),
             let url = dynamicLink.url else {
              return
          }
          handle(url: url)
      }
    

0
投票

我们的UAT应用程序在iOS应用程序关闭时无法跳转到深层链接页面的根本原因, 什么时候设置iOS配置站点: https://apps-uat.XXX.com/.well-known/apple-app-site-association。 但在这个 json 文件中,我们设置了更多的环境,

{ "applinks": { "apps": [], "details": [ { "appID": ...uat} ,{ "appID": ...dev}...]}} .

删除多余的设置后,DL再次正常工作。


-1
投票

我无法对此进行测试,但它可以帮助在应用程序关闭时处理动态链接。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    if let userActivityDict = launchOptions?[.userActivityDictionary] as? [UIApplication.LaunchOptionsKey : Any],
        let userActivity = userActivityDict[.userActivityType] as? NSUserActivity {

        // execute the dynamic url code here using the userActivity
    }

    return true
}
© www.soinside.com 2019 - 2024. All rights reserved.