4

I started to use https://branch.io/ in an iOS app.

The problem I am facing now is: how to catch the incoming link with its parameters?

Following the documentation here.

I got to this point, having this link:

http://myapp.app.link/A1bcDEFgHi?XP=315.0,419.0

and my code being:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    Branch *branch = [Branch getInstance];
    [branch initSessionWithLaunchOptions:launchOptions 
              andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
        if (!error && params) {
            NSLog(@"Here is params: %@",params.description); // I need to replace this, with code to get XP!
        }
    }];

    return YES;
}    


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

    return handledByBranch;
}

Here is what I get in the debugging console:

2016-06-10 11:59:44.407 TheApp[1786:591391] Here is params: {
    "$identity_id" = 2712491317999770034;
    "$one_time_use" = 0;
    "+click_timestamp" = 1465529273;
    "+clicked_branch_link" = 1;
    "+is_first_session" = 0;
    "+match_guaranteed" = 1;
    XP = "315.0,419.0";
    "~creation_source" = "iOS SDK";
    "~id" = 211224109823923327;
}

Now the question is, what is the code to use (inside the app) to get hold of the chunk of data: "315.0,419.0" ?

Michel
  • 8,287
  • 14
  • 60
  • 126

2 Answers2

9

Alex with Branch here: this is a good question, and one that we should probably make more clear in our docs!

When you generate a link (e.g., http://myapp.app.link/A1bcDEFgHi), you can set whatever data parameters you want and you'll get them back in your initSessionWithLaunchOptions() call (as documented here). Your callback data might look something like this:

{
    tags: [ 'tag1', 'tag2' ],
    channel: 'facebook',
    feature: 'dashboard',
    stage: 'new user',
    data: {
        mydata: 'something',
        foo: 'bar',
    }
}

If you append a query to the link URL (e.g., http://myapp.app.link/A1bcDEFgHi?XP=315.0,419.0) we just capture that parameter and pass it through to you:

{
    tags: [ 'tag1', 'tag2' ],
    channel: 'facebook',
    feature: 'dashboard',
    stage: 'new user',
    data: {
        mydata: 'something',
        foo: 'bar',
        XP: '315.0,419.0'
    }
}
Alex Bauer
  • 11,741
  • 1
  • 18
  • 35
  • Thanks for the tip, helping me to be on the right track. I now see that the application:continueUserActivity:restorationHandler: method is very important in the process. Though I got a big step further ahead, I still have a bit more to go. For that I edited the question, if you have time please take a look. – Michel Jun 10 '16 at 03:59
1

See this question: Best way to parse URL string to get values for keys?

There are also third party frameworks you can use to handle this. If you're using Cocoapods, go here and search for "Query".


UPDATE

I think you need to set up either URL Schemes or Universal Links in your app.

I have answered a previous question about URL Schemes here and you can checkout branch.io's documentation on Universal Links here


UPDATE 2

In your example output you would access XP as such params[@"XP"] but based on the other answer from Alex you would access it with params[@"data"][@"XP"]

Params is just a dictionary so you access it like you would any other dictionary in iOS.

Community
  • 1
  • 1
Hodson
  • 3,003
  • 1
  • 17
  • 44
  • Yes I have already done that. But how to catch the link from inside the app is what I am having trouble with. – Michel Jun 09 '16 at 15:12
  • @Michel - You seem to have got help form Alex. I updated my answer with some information on how you should be able to access your value. – Hodson Jun 10 '16 at 07:50
  • Rather than params[@"data"][@"XP"] it is [params objectForKey:@"XP"] which works. Anyway that was the idea, thanks. I wish the doc was more explicit sometime :) – Michel Jun 10 '16 at 08:15
  • I want to accept the answer. But it's a toughf one. Alex gave me the biggest help, but you hit the last nail :). Alex's answer + your Update 2 = Perfection. – Michel Jun 10 '16 at 08:21