2

Before posting this I tried this link Using App Links Hosting API for link shared on Facebook from iOS app with out any success. Here is my code

// I have declared my FBSession object here in app delegate.
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//Create dict params
NSDictionary *paramsForAppLinksHost = [NSDictionary dictionaryWithObjectsAndKeys:
                                       @"appName", @"name",
                                       @"myScheme://", @"al:ios:url",
                                       @"appStoreId", @"al:ios:app_store_id",
                                       @"appName", @"al:ios:app_name",
                                       @"{should_fallback:false}", @"web",
                                       nil
                                       ];


//Call graph API
FBRequest *request = [[FBRequest alloc] initWithSession:appDelegate.mpFacebookSession
                                              graphPath:@"/app/app_link_hosts"
                                             parameters: paramsForAppLinksHost
                                             HTTPMethod:@"GET"];

FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error)
{
    // output the results of the request
    [self postUrlCompleted:connection result:result error:error];
};
[newConnection addRequest:request completionHandler:handler];
[newConnection start];

Result:Printing description of result: { data = ( ); }

Error:nil.

What I am doing wrong here? This Facebook documentation for me really lacks details. Does this happens because of any permission issues?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nikhil.T
  • 241
  • 2
  • 13
  • When I checked FB docs it said something about we should use app token , or FB appId | APP_SECRET_KEY in app access token, is this applicable for my case also? why they aren't giving any specific informations... – Nikhil.T Mar 03 '15 at 12:30

1 Answers1

2

First of all I want to say that Facebook doc really sucks, it never really says anything in detail, after doing all these I came to know that app linking do not support by Mobile Safari:( . Which means this will not work if no Facebook app

So we left with two options.

  1. 1.If we implement app linking this will not work for people who don't have fb app.

  2. If we use URL and share to FB and clicking on it re-direct and use some schema by your server side to launch your app, this also not work if you have Facebook app installed because new FB app has its own browser, which doesn't seems to support redirection using document.location() . Also in iOS 8.0 redirection using document.location ()(which will use schema) not working for some reason. So basically I am forced to use app linking:(

Coming to my problem, it was due to the parameter mismatch but the API call doesn't say anything about it, I used following code to get App linking ID. As the link in my question for me calling it as in that answer didn't worked, so I used like this. Hope it save somebody's time.

//Get App Token, it is app token not user token

> NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];   
> NSString* pFaceBookAppID = [infoDict objectForKey:@"FacebookAppID"];  
> NSString *pStr = [NSString   
> stringWithFormat:@"https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=%@&client_secret={app_secret_key}",pFaceBookAppID];
> NSURL* pWebUrl = [NSURL URLWithString:pStr]; NSString *fullToken =   
> [NSString stringWithContentsOfURL:pWebUrl   
> encoding:NSUTF8StringEncoding error:nil]; NSArray *components =   
> [fullToken componentsSeparatedByString:@"="]; self.mpFBAppToken =   
> [components objectAtIndex:1];

//Now you can call like this to get app ID,

    NSArray *pArray = @[@{@"url":{URL_SCHEMA},@"app_store_id":{app_store_id},@"app_name":{{app_name}}];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:pArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSDictionary *pDict = @{@"should_fallback" :@"false"};
    jsonData = [NSJSONSerialization dataWithJSONObject:pDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonWebString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    pRequestString = [NSString stringWithFormat:@"access_token=%@&name={app_name}&ios=%@&web=%@",self.mpFBAppToken,jsonString,jsonWebString];

    NSURL* pWebUrl = [NSURL URLWithString:@"https://graph.facebook.com/app/app_link_hosts"];
    NSData *myRequestData = [NSData dataWithBytes:[pRequestString UTF8String] length:[pRequestString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:pWebUrl];
    [request setHTTPMethod:POST_METHOD];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:CONTENT_TYPE];
    [request setHTTPBody: myRequestData ];

    mpConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self] ;

// Now you have to implement the connection delegate functions to get the result, it will contain the app link ID, use that to get URL as below. To get URL..

    NSDictionary *pParams = @{@"access_token":self.mpFBAppToken};
    [FBRequestConnection startWithGraphPath:self.mpAPPLinkID
                                 parameters:pParams
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ){
                                                         }];

//Now in the URL just append the params you want to pass as query string, so that you can parse it easily like this from app delegate openURL method...

>  [FBAppCall handleOpenURL:url
>                              sourceApplication:sourceApplication
>                                fallbackHandler:^(FBAppCall *call)
>                                {
>                                    // Retrieve the link associated with the post
>                                    NSURL *targetURL = [[call appLinkData] targetURL];
>                                   if([[targetURL query]length] > 0)
>                                   {
//Parse Query string by some method;                         
>                                       NSMutableDictionary *pDict = [[NSMutableDictionary alloc]initWithDictionary:[self
> parseURLParams:[targetURL query]]]; } }];

Hope this helps some one.

Nikhil.T
  • 241
  • 2
  • 13