How to attach data to Facebook app request on the iPhone?

1.2k Views Asked by At

I'm working on 'deep linking' a Facebook request into my iOS app. A Facebook app is setup and seems to work as I can send a request to a friend, the request badge appears in the friend's Facebook, and clicking on the request launches my app (all on the iPhone). However, so far I could not pass any data with the request, which I want to use when my app launched from the Facebook app with the request.

I use the following call:

-(void) fbRequestActionWithMessage: (NSString *) message andLink: (NSString *) link
{
    NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"data1", @"key1",
                      @"data2", @"key2",
                      nil];

    NSString *requestDataString = [requestData JSONRepresentation];

    NSMutableDictionary* params = [NSMutableDictionary
                                  dictionaryWithObjectsAndKeys:
                                  message,  @"message",
                                  @"Check this out", @"notification_text",
                                  link, @"link",
                                  requestDataString, @"data",                                   
                                  nil];

    [facebook dialog:@"apprequests" andParams:params andDelegate:self];
}

Neither the "data" nor the "link" values in the params dictionary seem to have any effect. Ideally, when my app is launched from this request, I would get back the "data" or the "link" values. Can this be done? I could not find any Facebook docs about the structure of params dictionary - is there a list of supported keys and their effect?

1

There are 1 best solutions below

0
On

The "link" parameter and "notification_text" is not supported in iOS but you should be able to pass in data and get it back.

Example, passing in data:

FBSBJSON *jsonWriter = [FBSBJSON new];
NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"data1", @"key1",
                      @"data2", @"key2",
                      nil];

NSString *requestDataString = [jsonWriter stringWithObject:requestData];
NSMutableDictionary* params = 
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
    message, @"message",
    requestDataString, @"data",
    nil];

[facebook dialog:@"apprequests"
       andParams:params
     andDelegate:self];

Example, reading it back:

....
@property (nonatomic, retain) NSURL *openedURL;

....
@synthesize openedURL = _openedURL;

....
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    self.openedURL = url;
    return [FBSession.activeSession handleOpenURL:url]; 
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSession.activeSession handleDidBecomeActive];
    if (FBSession.activeSession.isOpen) {
        [self checkIncomingNotification];
    }
}

- (void) notificationGet:(NSString *)requestid {
    [FBRequestConnection startWithGraphPath:requestid
          completionHandler:^(FBRequestConnection *connection,
                              id result,
                              NSError *error) {
              if (!error) {
                  NSString *title;
                  NSString *message;
                  if ([result objectForKey:@"data"]) {
                      // Process data in request
                      FBSBJSON *jsonParser = [FBSBJSON new];
                      NSDictionary *requestData = 
                      [jsonParser 
                        objectWithString:[result objectForKey:@"data"]];
                      [NSString stringWithFormat:@"Badge: %@, Karma: %@",
                      NSString *data1 = [requestData objectForKey:@"key1"];
                      NSString *data2 = [requestData objectForKey:@"key2"];
                  }
              }
          }];
}

- (void) checkIncomingNotification {
  if (self.openedURL) {
    NSString *query = [self.openedURL fragment];
    if (!query) {
        query = [self.openedURL query];
    }        
    NSDictionary *params = [self parseURLParams:query];
    // Check target URL exists
    NSString *targetURLString = [params valueForKey:@"target_url"];
    if (targetURLString) {
        NSURL *targetURL = [NSURL URLWithString:targetURLString];
        NSDictionary *targetParams = [self parseURLParams:[targetURL query]];
        NSString *ref = [targetParams valueForKey:@"ref"];
        // Check for the ref parameter to check if this is one of
        // our incoming news feed link, otherwise it can be an
        // an attribution link
        if ([ref isEqualToString:@"notif"]) {
            // Get the request id
            NSString *requestIDParam = [targetParams 
                                        objectForKey:@"request_ids"];
            NSArray *requestIDs = [requestIDParam 
                                   componentsSeparatedByString:@","];

            // Get the request data from a Graph API call to the
            // request id endpoint
            [self notificationGet:[requestIDs objectAtIndex:0]];
        }
    }
    // Clean out to avoid duplicate calls
    self.openedURL = nil;
  }
}

You can find more details on this, using the latest SDK v3.1: https://developers.facebook.com/docs/howtos/send-requests-using-ios-sdk/