简体   繁体   中英

How to send Friend Request From iOS App

I want to send the friend request from my app. I used 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 dialog, when i click on confirm it will gives message Sorry something went wrong . We are working on getting this fixed as soon as we can.

I have integrated Facebook SDK successfully. I got my profile information and also my friends list. So Please help me to solve this.

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

replace this

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

EDIT:
change the method for calling

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

and refer here

check this code.

/* * Event: Done button clicked */

- (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 clicked */

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM