3

我在我的项目中使用 Branch.io 从我的网站启动应用程序。我想要的是如果应用程序关闭,当我单击网络上的通用链接时,它将首先打开主页。相反,它打开了另一个。它适用于 iOS 8。但在 iOS 9+ 中,它总是打开 LaunchScreen。

请看一下我的代码:

应用委托:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let branch = Branch.getInstance()
    branch.initSessionWithLaunchOptions(launchOptions) { (params, error) -> Void in
      DHIndicator.hide()
      if let _ = params {
        print("kdlkasdlf: \(params.debugDescription)")
        if let str = params["$deeplink_path"], url = NSURL(string: str as! String) {
          NSLog("link: \(url)")
          self.path = url.path
          self.query = url.query
          LaunchAppFlowManager.shareInstance.displayLaunchDetails()
        } else {
          // load your normal view
        }
      }
    }
window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.makeKeyAndVisible()
    window?.backgroundColor = UIColor.whiteColor()
    AccountFlowManager.shareInstance.start()
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    Branch.getInstance().handleDeepLink(url)
    if AppDelegate.shareInstance().window?.rootViewController?.nibName != "LaunchScreenVC" {
      DHIndicator.show()
    }
    return true
  }

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    print(userActivity.webpageURL?.absoluteString)
    if AppDelegate.shareInstance().window?.rootViewController?.nibName != "LaunchScreenVC" {
      DHIndicator.show()
    }
    return Branch.getInstance().continueUserActivity(userActivity)
  }

并且,启动应用程序的功能:

if let _ = AppDelegate.shareInstance().path, _ = AppDelegate.shareInstance().query {
        let navi = UINavigationController(rootViewController: HomePageVC())
        self.navi = navi
        AppDelegate.shareInstance().window?.rootViewController = navi
      } else {
        let vc = LaunchScreenVC()
        AppDelegate.shareInstance().window?.rootViewController = vc
      }
4

1 回答 1

0

总结一下这个问题:

  1. 在 iOS 8 上,当单击分支链接时,应用程序打开并显示正确的 ViewController
  2. 在 iOS 9 上,当单击分支链接时,应用程序会打开,但会显示默认的 ViewController

这种情况下的问题与通用链接有关。以前,在 iOS 8 上使用 URI 方案,您将使用 $deeplink_path 在您的应用程序内进行路由。而不是使用 $deeplink_path,Universal Links Branch 现在建议使用存储在自定义字段中的链接数据来通知应用内路由决策,如下所述:https ://dev.branch.io/getting-started/deep-link-routing /advanced/ios/#building-a-custom-deep-link-routing-method

此外,在 Branch init 完成块中找到用于应用内路由的代码也很重要,因为此完成块仅在收到来自 Branch 的服务器的响应并且链接参数可用后运行。如果应用内路由的代码位于完成块之外(并且不被完成块调用),则很可能会在 Branch 实际返回链接参数之前做出路由决定。

于 2017-02-17T18:22:59.230 回答