0
votes

I have successfully implemented deep linking in iOS app by Facebook. But I want to redirect the link opened by the Facebook post to a particular screen in my iOS app.

For this I am following this link : https://developers.facebook.com/docs/applinks/ios

So the openURL looks like this:

    - (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {


    NSString *callingUrl=[[NSUserDefaults standardUserDefaults] stringForKey:@"url"];
    if ([callingUrl isEqualToString:@"fb"]) {
        BOOL urlWasHandled =
        [FBAppCall handleOpenURL:url
               sourceApplication:sourceApplication
                 fallbackHandler:
         ^(FBAppCall *call) {
             // Parse the incoming URL to look for a target_url parameter

             NSString *query = [url query];  // **query is nil here**
             NSDictionary *params = [self parseURLParams:query];
             // Check if target URL exists
             NSString *appLinkDataString = [params valueForKey:@"al_applink_data"];
             if (appLinkDataString) {
                 NSError *error = nil;
                 NSDictionary *applinkData =
                 [NSJSONSerialization JSONObjectWithData:[appLinkDataString dataUsingEncoding:NSUTF8StringEncoding]
                                                 options:0
                                                   error:&error];
                 if (!error &&
                     [applinkData isKindOfClass:[NSDictionary class]] &&
                     applinkData[@"target_url"]) {
                     NSString *targetURLString = applinkData[@"target_url"];
                     // Show the incoming link in an alert
                     // Your code to direct the user to the
                     // appropriate flow within your app goes here
                     [[[UIAlertView alloc] initWithTitle:@"Received link:"
                                                 message:targetURLString
                                                delegate:nil
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil] show];
                 }
             }
         }];
        return urlWasHandled;
}

parseURLParams:

  - (NSDictionary*)parseURLParams:(NSString *)query {
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    for (NSString *pair in pairs) {
        NSArray *kv = [pair componentsSeparatedByString:@"="];
        NSString *val = [[kv objectAtIndex:1]
                         stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [params setObject:val forKey:[kv objectAtIndex:0]];
    }
    return params;
}

The problem is that, the url is not getting parsed. I have mentioned in the code above: query is nil.

[url query] - Here url is : fb654127854605525://authorize/#target_url=http%3A%2F%2FmyDomain%2FmyApp%2Fregister.php%3Fcampaign_id%3D999%26l_id%3D133%26share%3Dtrue%26customer_id%3DNDQ1%26domain%3D1

Where am I getting wrong? Is there anything wrong with the url?

3

3 Answers

1
votes

I had the same problem.

Replace:

NSString *query = [url query];

With:

NSString *query = [url absoluteString];

You should then have the url to parse.

0
votes

Use Bolts framework to parse URL. This framework is included in FBSDK since version 4.

0
votes

The example of code parsing FB URL using Bolts SDK (for RobP comment): ``

- (void)openURL:(NSURL *)url fromSource:(NSString *)source
{
    NSString *s = [url resourceSpecifier];
    if (([url query] == nil) && (s != nil))
    {
        NSRange r = [s rangeOfString:@"target_url="];
        if (r.location != NSNotFound)
        {
            url = [NSURL URLWithString:[[s substringFromIndex:(r.location + r.length)] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        }
    }
    BFURL *parsedUrl = [BFURL URLWithInboundURL:url sourceApplication:source];
    if (parsedUrl != nil)
    {
        NSLog(@"APP DATA=%@\nTARGET QUERY=%@\n",[[parsedUrl appLinkData] debugDescription],[[parsedUrl targetQueryParameters] debugDescription]);
        if ([parsedUrl targetQueryParameters] != nil)
        {
            NSString *objectId = [parsedUrl targetQueryParameters][@"action_object_map"];
            if (objectId == nil)
            {
                NSString *actionId = [parsedUrl targetQueryParameters][@"fb_action_ids"];
                if (actionId != nil)
                {
// Do something with FB Action Id
                }
            }
            else
            {
                objectId = [objectId substringWithRange:NSMakeRange(1, [objectId length] - 2)];
// Do something with FB Object Id
            }
        }
    }
}