iOS:如何获取Facebook相册照片选择器

13
Facebook iOS SDK是否提供任何默认的相册照片选择器,类似于UIImagePickerController
我听说过这个功能,但是长时间搜寻后仍未找到相关信息。
如果有人有任何想法,请告知我。如果没有这种功能,那么我肯定需要自己开发一个。
2个回答

25

从Facebook导入

无需第三方工具从图库中获取Facebook照片。支持Graph API 2.4、2.5、2.6、2.7、2.8和Facebook Graph API 2.10。

1)在Facebook开发者账户中添加应用程序

2)然后在应用程序中选择平台,如下图所示

enter image description here

3)选择iOS并按向导中显示的所有步骤进行操作

4)然后转到Graph API Explorerhttps://developers.facebook.com/tools/explorer/145634995501895/

5)在Graph API Explorer中选择您的应用程序以了解更多信息,请参阅以下屏幕截图

enter image description here

6)单击“获取令牌”..然后单击“获取访问令牌”,在“获取令牌”中,您可以看到很多权限,以便更好地了解查看图像

enter image description here

7)要检查是否已打开或关闭"user_photos"权限以获取相册名称和图像,请显示以下屏幕截图

enter image description here

如果未打开“user_photos”权限,则显示错误,如下所示

enter image description here

完成第7步后,继续下面的步骤或只需下载编码文件。我放了一个链接供您下载

Objective-C的编码文件

https://www.dropbox.com/s/1ysek035ek88xuw/FacebookAlbum%20Demo.zip?dl=0

8)如果没有错误,请在视图控制器的viewDidLoad方法中使用以下代码,该代码用于获取相册名称和相册ID。

Objective-C代码

     FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];

    [loginManager logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends",@"user_photos"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)

    {

        [[[FBSDKGraphRequest alloc]
          initWithGraphPath:@"me/albums"
          parameters: nil
          HTTPMethod:@"GET"]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {

               //  NSLog("%@",result);

                 NSArray *data = [result valueForKey:@"data"];


                 name = [data valueForKey:@"name"];

                 ids = [data valueForKey:@"id"];


                 [self.Fbtableview reloadData];


             }


         }];

9) 在 tableView cellForRow 方法中添加以下代码以获取专辑封面照片。

Objective-C

 coverid = [NSString stringWithFormat:@"/%@?fields=picture",[ids objectAtIndex:indexPath.row]]; **// pass album ids one by one**


/* make the API call */
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                              initWithGraphPath:coverid
                              parameters:nil
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error)
 {

    // NSLog("%@",result);

     NSDictionary *pictureData  = [result valueForKey:@"picture"];

     NSDictionary *redata = [pictureData valueForKey:@"data"];

     urlCover = [redata valueForKey:@"url"];



     NSURL *strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",urlCover]];

     NSData *data = [NSData dataWithContentsOfURL:strUrl];
     UIImage *img = [[UIImage alloc] initWithData:data];
     UIImageView *img1;

     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
     {
         img1= [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 50, 50)];
     }
     else
     {
         img1= [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 40, 40)];
     }



     [img1 setImage:img];
     [img1 setContentMode:UIViewContentModeScaleAspectFit];
     [cell.contentView addSubview:img1];


 }];

10)现在编写代码获取相册照片的ID。

 // code in **table didselect method**

Objective-C

   NSString *strAlbumid = [NSString stringWithFormat:@"/%@/photos",[ids objectAtIndex:indexPath.row]];



FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                              initWithGraphPath:strAlbumid
                              parameters:nil
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {

    // NSLog("%@",result);
    NSDictionary *data = [result valueForKey:@"data"];

    arrId = [data valueForKey:@"id"];



    PhotoViewControllerr *photoViewcontroller;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        photoViewcontroller = [[PhotoViewControllerr alloc]initWithNibName:@"PhotoViewController_Ipad" bundle:nil];
    }
    else
    {
        photoViewcontroller = [[PhotoViewControllerr alloc]initWithNibName:@"PhotoViewController" bundle:nil];
    }


    photoViewcontroller.picsArray = arrId;

    [photoViewcontroller.navigationController setNavigationBarHidden:YES];




    [[self navigationController] pushViewController:  photoViewcontroller animated:YES];


}];

11) 另一个视图控制器现在需要在collection view cellforRow中获取图像的代码。

Objective-C

NSString *strAlbumid = [NSString stringWithFormat:@"/%@/?fields=images",[self.picsArray objectAtIndex:indexPath.row]];



FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                              initWithGraphPath:strAlbumid
                              parameters:nil
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error)
{

    //NSLog("%@",result);
    picture_images = [result valueForKey:@"images"];

    NSMutableArray *picCount = [picture_images objectAtIndex:picture_images.count - 1];



     NSURL *strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[picCount valueForKey:@"source"]]];


    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(q, ^{
        /* Fetch the image from the server... */
       NSData *data = [NSData dataWithContentsOfURL:strUrl];
         UIImage *img = [[UIImage alloc] initWithData:data];
         dispatch_async(dispatch_get_main_queue(), ^{

         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
         {

         img1= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
         }
         else
         {

         img1= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
         }



         img1.image = img;
         img1.contentMode = UIViewContentModeScaleAspectFit;
         [cell.contentView addSubview:img1];

         });
         });



}];

12) 在获取数据之后,您必须在Facebook上提交您的应用程序。为了提交,您可以填写以下信息:

1)应用程序详细信息

2)状态和审核

=====

如果您有任何疑问,我将帮助您。

享受快乐编码。


谢谢您的回复,但我如何使用静态用户登录? - Skovie
我想手动输入用户名和密码,这样我的视图控制器就不会显示登录弹窗,而是始终从我设置的 Facebook 页面获取相册。 - Skovie
请问您可以分享Swift文件吗? - Kunal Gupta
@Maulik,你有Swift 3的代码吗?我快完成了,但是在我的照片中出现了一个不寻常的错误。 - Anurag Sharma

2

已测试,以下是有关编程的内容的翻译。 - Maulik shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接