ios - 如何在 ios 9 中使用 facebook sdk 4.7 获取用户的电子邮件 ID

标签 ios objective-c ios9.1 fbsdk

这是我的代码,我只获取用户的用户名和 facebook id。

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login
     logInWithReadPermissions: @[@"public_profile",@"email",@"user_friends"]
     fromViewController:self
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
             NSLog(@"Process error");
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
         } else {

             if ([FBSDKAccessToken currentAccessToken]) {
                 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
                  startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                      if (!error) {
                          NSLog(@"fetched user:%@", result);
                      }
                  }];
             }             NSLog(@"Results=%@",result);
             NSLog(@"Logged in");
         }
     }];

它也会给出一些像这样的错误-

-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)" -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"

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

最佳答案

我正在为 Facebook sdk 使用 pod,它在 iOS9.0 和 xcode 7.1 中工作。试试这段代码

    void (^sendRequestsBlock)(void) = ^{

    FBSDKGraphRequest *postRequest = nil;
    NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"id,email,first_name,last_name,name,picture{url}" forKey:@"fields"];

    postRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters HTTPMethod:@"GET"];
    [postRequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
     {
         if (!error) {
             NSDictionary *dictResult = (NSDictionary *)result;

         }
    }];
};

/// login start
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];
NSArray *permissions = [NSArray arrayWithObjects:@"email",@"public_profile", nil];

[login logInWithReadPermissions:permissions fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    if (error) {

    } else if (result.isCancelled) {

    } else {

        if ([result.grantedPermissions containsObject:@"email"]) {

            sendRequestsBlock();

        }
    }
}]

关于ios - 如何在 ios 9 中使用 facebook sdk 4.7 获取用户的电子邮件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513630/

相关文章:

ios - 如何在 UIAlertAction 中传递多个处理程序

ios - 向核心数据对象或属性添加弃用

iphone - 在 UISplitViewController 中设置主视图的显示/隐藏启用,仅用于横向模式下的一个 View Controller

ios - 单击更改 BarButtonItem 图标

xcode7 - 该设备当前不在线

ios - "Could not find Developer Disk Image"

ios - updateValue 不适用于字典

ios - 在 IOS 中水平拆分 UITableview?

ios - 在 Swift 中绘制多边形

iphone - 如何获取指向 id 变量和 NSObject 指针的指针?