【问题标题】:Facebook sdk fetch friend errorFacebook sdk 获取好友错误
【发布时间】:2013-07-13 02:50:32
【问题描述】:

我是 iOS 开发的新手。使用 Facebook SDK 从 facebook 获取朋友时出错。这是我的代码。

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

视图控制器

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

输出

##Begin
##End
Json data

请帮我在显示 ##End.Thanks 后获取 Json 数据

【问题讨论】:

    标签: iphone ios facebook ios6


    【解决方案1】:

    这里有一个获取朋友信息的代码示例:

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

    记住:

    实现正确的委托方法 在获取好友信息之前,您必须调用 authorize。这样用户就可以先登录。 我希望这会有所帮助,干杯

    【讨论】:

    • 您好 Anjaneyulu,谢谢您的回复。我无法理解,您能给我举个例子吗?抱歉,iOS 新手。谢谢。
    • 是的,我在完成打印后得到字典响应 ##Begin ##End at the pickFriendPressed at viewcontroller。
    【解决方案2】:

    试试这个:

    获取好友列表使用

    [FBRequest requestForMyFriends];

    FBRequest* friendsRequest = [FBRequest requestForMyFriends];

    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection connection,NSDictionary result,NSError error) { NSArray friends = [result objectForKey:@"data"]; ......

    代码如下,但主行是[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];
        }
    }
    

    How to get the list of friend without opening FBFriendPickerViewController iOS上查看我接受的这个答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多