Branch.io - 打开应用程序但未获取数据的链接

问题描述:

如果应用程序已打开,Branch.io链接不会获取数据。当应用程序安装链接直接打开应用程序,但数据丢失。但是,如果我重定向到应用程序商店,然后再次点击分支链接,并直接打开应用程序,然后数据来。请指导,发布下面的代码。Branch.io - 打开应用程序但未获取数据的链接

更新:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    let branch: Branch = Branch.getInstance() 
    branch.setDebug() 
    branch.initSessionWithLaunchOptions(launchOptions, andRegisterDeepLinkHandler: {params, error in 
     if error == nil { 
      // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app 
      // params will be empty if no data found 
      print("params: %@", params.description) 
      print(params["event_id"]) 
      if let url = params["event_id"] as? NSInteger { 
       let strEventID = String(url) 
       print(strEventID) 


    }) 
    self.window?.rootViewController = nav 
    self.window?.makeKeyAndVisible() 
    return true 
} 

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { 

    Branch.getInstance().handleDeepLink(url) 

    if(sourceApplication == "com.linkedin.LinkedIn") 
    { 
     if(LISDKCallbackHandler.shouldHandleUrl(url)) 
     { 
      return LISDKCallbackHandler.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) 
     } 
    } 

    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) 
} 

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool { 
    // handler for Universal Links 
    Branch.getInstance().continueUserActivity(userActivity) 
    return true 
} 

// Called when a notification is received and the app is in the 
// foreground (or if the app was in the background and the user clicks on the notification). 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) 
{ 
    Branch.getInstance().handlePushNotification(userInfo) 
    if(UserSingleton.sharedInstance.accessToken != kEmptyString) 
    { 
     if let aps = userInfo["aps"] as? NSDictionary { 

      var title = kEmptyString 
      var message = kEmptyString 
      var inviteID = kEmptyString 
      var statusType = kEmptyString 

      if let inviteIDLocal = aps.valueForKey("id") as? NSNumber 
      { 
       inviteID = "\(inviteIDLocal)" 
      } 

      else if let inviteIDLocal = aps.valueForKey("id") as? String 
      { 
       inviteID = inviteIDLocal 
      } 
      else 
      { 
       inviteID = "0" 
      } 

      if let statusTypeLocal = aps.valueForKey("status_type") as? String 
      { 
       statusType = statusTypeLocal 
      } 

      if let titleLocal = aps.valueForKey("alert")?.valueForKey("title") as? String 
      { 
       title = titleLocal 
      } 

      if let messageLocal = aps.valueForKey("alert")?.valueForKey("body") as? String 
      { 
       message = messageLocal 
      } 
      CommonFunctions.addNotificationBar(title,message: message, inviteID: inviteID,statusType: statusType) 
     } 
    } 
} 
+0

这听起来像是一个'continueUserActivity'实现的问题。你能否从你的AppDelegate文件发布整个'didFinishLaunching'方法? –

+0

sure @AlexBauer请检查更新的问题 –

的一天时光后,终于知道原因。来自branch.io link的糟糕工作。他们的代码有问题。

// Respond to Universal Links 
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([**Any**]?) -> Void) -> Bool { 
// pass the url to the handle deep link call 
Branch.getInstance().continue(userActivity) 

return true 
} 

“Any”实际上应该是“AnyObject”。

+0

这实际上*不正确*。分支文档对于Swift 3是准确的,正如可以在官方[来自Apple的此方法的API参考条目](https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623072-application)中看到的那样。但是,在这种方法中使用'AnyObject'对于Swift 2.3是正确的。有关语法差异的更多信息[here](http://stackoverflow.com/questions/40673319/ios-swift-2-3-correct-syntax-for-application-restorationhandler)。对不起,你遇到了麻烦! –

+0

@AlexBauer在这个文档中没有提到这个代码为swift 3,因为像我这样在swift 2上工作的人没有开始快速3会遇到麻烦。在这里我也没有得到任何错误,所以这是错误的,在文档中这样的事情必须指定。休息你知道更好,谢谢澄清。 –

+0

@AlexBauer你可以引导我,链接不打开应用程序,也可以获取数据,但是当应用程序关闭并从链接开始时,它不会传递任何数据,并且应用程序正常重新启动(而不是链接)数据会自动传递。 –