How to send friend request from iOS app

I want to send a friend request from my application. I used the following code

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"My Title", @"title",
                                   @"Come check out my app.",  @"message",
                                   FrienduserId, @"id",
                                   nil];
[FBWebDialogs 
 presentDialogModallyWithSession:nil 
 dialog:@"friends" 
 parameters:[params mutableCopy] 
 handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error){
    if (error) {
        NSLog(@"%@",error);
    }
    else
    {
        NSLog(@"done");
    }
 }];

and it displays a dialog box, when I click on confirmation, it will display a message Sorry, something went wrong. We are working to fix this as soon as possible.

I have successfully integrated the SDK into Facebook. I received information about my profile, as well as a list of my friends. So please help me solve this problem.

+3
source share
3 answers
NSLog(@"%@",[app.Arr_Facebook_Frnd objectAtIndex:indexpath]);

NSString *str_id;
NSString *str_name;
NSString *str_link;

    str_id = [[app.Arr_Facebook_Frnd objectAtIndex:indexpath] objectForKey:@"id"];
    str_name = [[app.Arr_Facebook_Frnd objectAtIndex:indexpath] objectForKey:@"name"];
    str_link = @"www.google.com";


NSDictionary *params = @{
                         @"name" : str_name,
                         @"caption" : @"",
                         @"description" : @"",
                         @"picture" : @"",
                         @"link" : str_link,
                         @"to":str_id,
                         };

// Invoke the dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
                                       parameters:params
                                          handler:
 ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
     if (error) {
         NSLog(@"Error publishing story.");
         [self.indicator stopAnimating];
     } else {
         if (result == FBWebDialogResultDialogNotCompleted) {
             NSLog(@"User canceled story publishing.");
             [self.indicator stopAnimating];
         } else {
             NSLog(@"Story published.");
             [self.indicator stopAnimating];
         }
     }}];
+2
source

replace it

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"My Title", @"title",
                                   @"Come check out my app.",  @"message",
                                   FrienduserId, @"to",
                                   nil];

EDIT:
change the call method

[FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler:^()];

and send here

+1
source

check this code.

/ * * Event: Done button pressed * /

- (void)facebookViewControllerDoneWasPressed:(id)sender {
    FBFriendPickerViewController *friendPickerController =
    (FBFriendPickerViewController*)sender;
    NSLog(@"Selected friends: %@", friendPickerController.selection);
   // Dismiss the friend picker
[[sender presentingViewController] dismissViewControllerAnimated:YES completion:^{

    NSMutableString *text=[[NSMutableString alloc] init];
    for (id<FBGraphUser> user in friendPickerController.selection) {
        if ([text length]) {
            [text appendString:@", "];
        }
        [text appendFormat:@"%@",user.id];
    }
    [self friendSelectionDone:text.length > 0 ? text : @"<None>"];
}];
}

/ * * Event: Cancel button pressed * /

- (void)facebookViewControllerCancelWasPressed:(id)sender {
    NSLog(@"Canceled");
   // Dismiss the friend picker
   [[sender presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark
-(void)friendSelectionDone:(NSString*)userId{

if ([userId length]<1) {

    return;
}

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"Check this app out...",  @"message",
                               userId, @"to",
                               nil];
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                              message:[NSString stringWithFormat:@"Come and check out the App"]
                                                title:nil
                                           parameters:params
                                              handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                                                  if (error) {
                                                      // Case A: Error launching the dialog or sending request.
                                                      NSLog(@"Error sending request.");
                                                  } else {
                                                      if (result == FBWebDialogResultDialogNotCompleted) {
                                                          // Case B: User clicked the "x" icon
                                                          NSLog(@"User canceled request.");
                                                      } else {
                                                          NSLog(@"Request Sent.");

                                                          UIAlertView *alert=[[UIAlertView alloc] initWithTitle:APP_NAME
                                                                                                        message:@"Request Sent to your friends"
                                                                                                       delegate:nil
                                                                                              cancelButtonTitle:@"OK"
                                                                                              otherButtonTitles:nil, nil];
                                                          [alert show];
                                                      }
                                                  }}];

}
+1
source

All Articles