Swati Gupta Swati Gupta - 2 months ago 19
Objective-C Question

Branch.io Deeplink is opening the deep link every time the app is launched

I have used Branch.io for creating deeplink for my app. But it's redirecting me to the deep link controller every time app is launched.

I have used following code in -

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


Branch *branch = [Branch getInstance];

HomeDetailsViewController *controller = (HomeDetailsViewController*)[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"HomeDetailsViewControllerID"];

[branch registerDeepLinkController:controller forKey:@"bucketId"];
[branch initSessionWithLaunchOptions:launchOptions automaticallyDisplayDeepLinkController:YES];


..

// Respond to Universal Links - Branch io
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
BOOL handledByBranch = [[Branch getInstance] continueUserActivity:userActivity];

return handledByBranch;
}

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options NS_AVAILABLE_IOS(9_0) {

[[Branch getInstance] handleDeepLink:url];

[self application:app
processOpenURLAction:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
iosVersion:9];

return YES;
}



-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
[[Branch getInstance] handleDeepLink:url];

[self application:application
processOpenURLAction:url
sourceApplication:sourceApplication
annotation:annotation
iosVersion:8];

return YES;
}

Answer

You should not call instantiateViewController... from

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

According to their docs, you should use the following code instead:

Branch *branch = [Branch getInstance];
[branch initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
    if (!error && params) {
        // 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
        // ... insert custom logic here ...
        NSLog(@"params: %@", params.description);
    }
}];