Tuesday, 4 March 2014

iPhone Developement: Send app request to Facebook friends in IOS






Developing a game in IOS and want to send app request to your friends so that they can download it and play your niche game. Don't worry it's a easy process to send app request to your friends on Facebook. In IOS Facebook SDK allows us to open up a dialogue that contains logged-in user friends list, otherwise prompt user to log-in to send app request to friends. For this you can take use of FBWebDialogs class provided in Facebook SDK for IOS.

To display a dialogue you  have to call method presentRequestsDialogModallyWithSession, and upon respective user action it will return you result. The cod for opening dialogue or sending app request is shown below as shown below

 [FBWebDialogs
     presentRequestsDialogModallyWithSession:nil
     message:@"Learn how to make your iOS apps social."
     title:nil
     parameters:nil
     handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
         if (error) {
             // Error launching the dialog or sending the request.
             NSLog(@"Error sending request.");
         } else {
             if (result == FBWebDialogResultDialogNotCompleted) {
                 // User clicked the "x" icon
                 NSLog(@"User canceled request.");
             } else {
                 // Handle the send request callback
                NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
                 if (![urlParams valueForKey:@"request"]) {
                     // User clicked the Cancel button
                     NSLog(@"User canceled request.");
                 } else {
                     // User clicked the Send button
                     NSString *requestID = [urlParams valueForKey:@"request"];
                     NSLog(@"Request ID: %@", requestID);
                 }
             }
         }
     }];



- (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[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        params[kv[0]] = val;
    }
    return params;
}


You can see app request sent by your app by navigating to app center
https://www.facebook.com/appcenter/requests

Not seeing notification of your app on top right notification icon of Facebook, you will not until this condition met true with your app

Only apps that are actually on facebook.com can use the Notifications API. Note this is different than "app requests". In other words, you can't get your app to post a notification in the top right corner where the globe is.

NOTE:- If your message length is too long then dialogue will popup for a fraction and close automatically, so to let it behave properly change your message.

1 comment:
Write comments