【问题标题】:Get profile picture for other Facebook users in iOS在 iOS 中获取其他 Facebook 用户的头像
【发布时间】:2018-07-14 19:51:08
【问题描述】:

我正在尝试使用 Facebook SDK for iOS 中的 Graph API 获取 Facebook 用户的个人资料图片,但该网站的示例代码对我不起作用。 https://developers.facebook.com/docs/graph-api/reference/user/picture/

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
    initWithGraphPath:@"/123456789/picture"
           parameters:nil
           HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    // Insert your code here
}];

我在控制台中收到以下错误。

FBSDKLog: starting with Graph API v2.4, GET requests for //123456789/picture should contain an explicit "fields" parameter

根据我对错误消息而不是parameters:nil 的理解,我需要输入类似parameters:@{@"fields": @"id"} 的内容,但无论我在完成处理程序中尝试result 是否始终为零。我不知道为个人资料图片输入什么。

编辑: 我正在寻找其他用户的头像。

【问题讨论】:

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


    【解决方案1】:

    试试这个:-

    - (IBAction)btnFaceBook:(id)sender {
    
    
    
        FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
        [login setLoginBehavior:FBSDKLoginBehaviorBrowser];
        [login logInWithReadPermissions:@[@"public_profile",@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
            if (error)
            {
                [[FBSDKLoginManager new]logOut];
    
            } else if (result.isCancelled)
            {
                // Handle cancellations
            }
            else
            {
                if ([result.grantedPermissions containsObject:@"email"])
                {
                    [self getDataFromFB];
                }
            }
        }];
    }
    
    
    -(void)getDataFromFB
    {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view  animated:true];
    
        if ([FBSDKAccessToken currentAccessToken]) {
             [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"political,picture.width(500).height(500),id,email,first_name,last_name,gender,name" forKey:@"fields"]]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
             {
                 if (!error && [result count] != 0)
                 {
                     [hud hideAnimated:true];
                     NSLog(@"%@", result);
    
                 }
                 else
                 {
                      [hud hideAnimated:true];
                      [self displayAlertMesage:@"Some error has been occure." title:@"Oops!"];
                 }
            }];
         }
     }
    

    【讨论】:

    • 感谢您的回答,但我希望获取其他未登录我的应用的 Facebook ID 用户的个人资料图片。
    • @BerryBlue 你已经实现了用 facebook 登录?
    【解决方案2】:

    您可以使用以下代码从 facebook 登录获取数据:-

    -(void)getFacebookProfileInfos { FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" 参数:@{@"fields": @"id,email,name,birthday,first_name,last_name,gender,picture.type(large)"}];

    FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
    
    [connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    
        if(result)
        {
            dictUserData = [[NSDictionary alloc]init];
            dictUserData=result;
        }
    }];
    [connection start];
    

    }

    您将获得“dictUserData”字典中的所有数据,然后您将通过以下方式访问 facebook 个人资料:-

    UIImage * imgProfile = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", dictUserData[@"picture"][@"data"][@"url"]]] ]]; NSString * strImageData = [self imageToNSString:imgProfile];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多