简体   繁体   English

Facebook SDK获取朋友错误

[英]Facebook sdk fetch friend error

I am new to iOS development. 我是iOS开发的新手。 I got an error when fetching the friend from facebook by using the Facebook SDK.Here are my code. 使用Facebook SDK从Facebook获取朋友时出现错误,这是我的代码。

FacebookClass Facebook类

- (void)fetchFacebookFriend{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"user_birthday",@"friends_hometown",
                            @"friends_birthday",@"friends_location",@"friends_work_history",
                            nil];

    if (!FBSession.activeSession.isOpen) {
        // if the session is closed, then we open it here, and establish a handler for state changes
        [FBSession openActiveSessionWithReadPermissions:permissions
                                           allowLoginUI:YES
                                      completionHandler:^(FBSession *session,
                                                          FBSessionState state,
                                                          NSError *error) {

                                          if (error != nil) {
                                              NSLog(@"Failed to retrive facebook friend.");
                                              return;
                                          }
                                          else{

                                              self.myArray = [[NSMutableArray alloc] init];
                                              NSLog(@"permissions::%@",FBSession.activeSession.permissions);

                                              FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,picture,birthday,hometown,location,work"];
                                              [friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                                  appDelegate.myFacebookArray = [[NSMutableArray alloc] init];
                                                  for (NSDictionary *fbData in [result objectForKey:@"data"]) {
                                                      NSLog(@"%@",fbData);
                                                      [self.myArray addObject:fbData];

                                                  }

                                              }];


                                          }
                                      }];
        return;
    }
}

ViewController ViewController

- (IBAction)pickFriendPressed:(id)sender {
    NSLog(@"##Begin");
    [[FacebookClass sharedInstance] fetchFacebookFriend];
    NSLog(@"##End");
}

Output 输出量

##Begin
##End
Json data

Please help I get the Json data after display the ##End.Thanks 显示## End后请帮助我获取Json数据。谢谢

Here you have a code sample that fetches the friends info: 在这里,您有一个获取朋友信息的代码示例:

    [facebook requestWithGraphPath:@"me/friends" 
                         andParams:[ NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name,link,gender,last_name,first_name",@"fields",nil]
                       andDelegate:self];

Remember to: 记得:

implement the proper delegate methods You must call authorize before fetching the friends information. 实现正确的委托方法在获取朋友信息之前,您必须调用authorize。 This way the user will be able to login first. 这样,用户将能够首先登录。 I hope this helps, cheers 希望这对您有所帮助

Try this: 尝试这个:

get friend list using 使用获取好友列表

[FBRequest requestForMyFriends]; [FBRequest requestForMyFriends];

FBRequest* friendsRequest = [FBRequest requestForMyFriends]; FBRequest * friendsRequest = [FBRequest requestForMyFriends];

[friendsRequest startWithCompletionHandler: ^(FBRequestConnection connection,NSDictionary result,NSError error) { NSArray friends = [result objectForKey:@"data"]; [friendsRequest startWithCompletionHandler:^(FBRequestConnection连接,NSDictionary结果,NSError错误){NSArray friends = [result objectForKey:@“ data”]; ...... ......

the coding is as follow but main line is [FBRequest requestForMyFriends]; 编码如下,但主线是[FBRequest requestForMyFriends];

-(void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error {
    switch (state) {
        case FBSessionStateOpen: {
            if (self != nil) {
                [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                    if (error) {
                        //error
                    }else{
                        FBRequest* friendsRequest = [FBRequest requestForMyFriends];
                        [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error) {
                            NSArray* friends = [result objectForKey:@"data"];

                            for (int i = 0; i < [arrFacebookFriends count]; i++) {
                                UserShare *shareObj = [arrFacebookFriends objectAtIndex:i];
                                [shareObj release];
                                shareObj = nil;
                           }
                           [arrFacebookFriends removeAllObjects];

                            for (NSDictionary<FBGraphUser>* friend in friends) {
                                UserShare *shareObj = [[UserShare alloc] init];
                                shareObj.userName = friend.name;
                                shareObj.userFullName = friend.username;
                                shareObj.userId = [friend.id intValue];
                                NSLog(@"%@",friend.id);
                                shareObj.userPhotoUrl = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?", friend.id];
                                [arrFacebookFriends addObject:shareObj];
                                [shareObj release];
                            }
                            [self StopSpinner];
                            [tblFacebookFriends reloadData];
                        }];
                     }
                }];
            }
            FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController cacheDescriptor];
            [cacheDescriptor prefetchAndCacheForSession:session];
        }
            break;
        case FBSessionStateClosed: {
            [self StopSpinner];
            UIViewController *topViewController = [self.navigationController topViewController];
            UIViewController *modalViewController = [topViewController modalViewController];
            if (modalViewController != nil) {
                [topViewController dismissViewControllerAnimated:YES completion:nil];
            }
            //[self.navigationController popToRootViewControllerAnimated:NO];

            [FBSession.activeSession closeAndClearTokenInformation];

            [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.5f];
        }
            break;
        case FBSessionStateClosedLoginFailed: {
            [self StopSpinner];
            [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.5f];
        }
            break;
        default:
            break;
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:SCSessionStateChangedNotificationFL object:session];

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Error: %@", [FacebookFriendsListViewController FBErrorCodeDescription:error.code]] message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
}

see my accepted this answer on How to get the list of friend without opening FBFriendPickerViewController iOS 不打开FBFriendPickerViewController iOS的情况下如何获取朋友列表中看到我的已接受此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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