URL parsing result from Facebook Request API is correct

I wrote the following code that sends a predefined GraphAPI object to another Facebook user in my iOS app. It works well.

-(void)FacebookSendObjectWithIdentifier: (NSString *)objectIdentifier withObjectName:(NSString *)objectName withMessage:(NSString *)message withTitle:(NSString *)title withActionType:(NSString *)actionType runOnComplete:(void (^)(NSDictionary* request))completionBlock{

NSMutableDictionary* params =   [NSMutableDictionary dictionaryWithObjectsAndKeys:

                                 // Optional parameter for sending request directly to user
                                 // with UID. If not specified, the MFS will be invoked
                                 //@"RECIPIENT_USER_ID", @"to",

                                 // Give the action object request information
                                 actionType, @"action_type",
                                 objectIdentifier, @"object_id",
                                 nil];

[FBWebDialogs
 presentRequestsDialogModallyWithSession:nil
 message:message
 title:title
 parameters:params
 handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
     if (error) {
         // Error launching the dialog or sending the request.
         NSLog(@"Error sending %@ request.", objectName);
     } else {
         if (result == FBWebDialogResultDialogNotCompleted) {
             // User clicked the "x" icon
             NSLog(@"User canceled %@ request.", objectName);
         } 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.", objectName);
             } else {
                 // User clicked the Send button
                 NSString *requestID = [urlParams valueForKey:@"request"];
                 NSLog(@"%@ request sent, request id: %@", objectName, requestID);

                 if (completionBlock)
                     completionBlock(urlParams);
             }
         }
     }
 }];
}

      

[self parseURLParams: [resultURL query]]; implemented across multiple pages of Facebook documentation as follows:

- (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;
}

      

On return, the completion block parses the returned urlParams and stores them in our database for a link to the object submitted via Facebook. However, this should return a "request" parameter, which contains the request ID, and an "to" array, which contains the Facebook IDs of the people to whom the object was sent. Instead, it returns two fields in the url, "request", which works fine, and "to [0]" instead of "to" as an NSString instead of an NSArray, since the above method parses URL parameters as strings and not matching objects. which turn the array "in" in "before [0]" NSString.

I'm looking for either a better solution to parse the returned url from Facebook, or a better way to send an object through the Facebook API. I prefer the first option as it is the only step missing to complete the implementation.

Thank.

+3


source to share





All Articles