Facebook iOS SDK - 获取用户相册

时间:2014-08-18 13:49:41

标签: ios objective-c facebook facebook-graph-api

我在我的iPhone应用程序中使用Facebook SDK。我想找到用户的Facebook个人资料照片,因为我使用以下代码:

-(IBAction)FacebookLogin:(id)sender{
    if (FBSession.activeSession.isOpen) {

        [self findAlbums];
    } else {
        NSArray *permissions = [[NSArray alloc] initWithObjects:@"user_photos",
                                nil];

        [FBSession openActiveSessionWithPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState status,
                                                      NSError *error) {
                                      // if login fails for any reason, we alert
                                      if (error) {


                                      } else if (FB_ISSESSIONOPENWITHSTATE(status)) {

                                          [[FBRequest requestForMe] startWithCompletionHandler:
                                           ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                                               if (!error) {
                                                   [self findAlbums];

                                               }
                                           }];
                                      }
                                  }];
    }
}

-(void)findAlbums {
    [FBRequestConnection startWithGraphPath:@"/me/albums"
                                 parameters:nil
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {
                              /* handle the result */
                              NSLog(@"result::%@",result);
                          }];

}
  

输出---- data =();

这不会在数据中提供任何相册。登录的Facebook用户在他的个人资料中有许多专辑和照片。为什么会这样?

2 个答案:

答案 0 :(得分:2)

只需将查找相册中的代码更改为:

        [FBRequestConnection startWithGraphPath:@"me/albums"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if (!error) {
                              // Success! Include your code to handle the results here
                              NSLog(@"user events: %@", result);
                              NSArray *feed =[result objectForKey:@"data"];

                              for (NSDictionary *dict in feed) {

                                  NSLog(@"first %@",dict);

                              }
                          } else {
                              // An error occurred, we need to handle the error
                              // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                              NSLog(@"error %@", error.description);
                          }
 }];

答案 1 :(得分:0)

您必须将Facebook正在使用的Facebook凭据(即Facebook帐户)添加到Facebook开发者帐户设置(即管理员或开发人员),然后才能通过代码访问相册的照片