2016-12-14 96 views
2

如果應用程序已打開,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請檢查更新的問題 –

回答

-2

的一天時光後,終於知道原因。來自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你可以引導我,鏈接不打開應用程序,也可以獲取數據,但是當應用程序關閉並從鏈接開始時,它不會傳遞任何數據,並且應用程序正常重新啓動(而不是鏈接)數據會自動傳遞。 –

相關問題