Search code examples
iosswiftios9branch.io

Branch.io - link opening the app but not getting data in it


Branch.io link not getting data if app opened installed already. When app installed link opens the app directly but data missing. But if i redirect to app store then click again on branch link and open the app directly then data comes. Please guide, posting the code below.

Update:

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)
        }
    }
}

Solution

  • After spending an day, finally came to know the reason.

    // 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" should be actually "AnyObject".