如何在ios 9中使用facebook sdk 4.7获取用户的电子邮件ID
问题描述:
这是我的代码,我只获取用户名和用户的Facebook ID。
Here is my code , I am only getting user name and facebook id of user.
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");
}
}];
并且它也会出现这样的错误 -
and it also give some error like this -
-canOpenURL:URL失败:fbauth2:/ - 错误:(null) - scanOpenURL:URL失败:fbauth2:/ - 错误:(null)
-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)" -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
FBSDKLog:从Graph API v2.4开始,/ b的GET请求应该是
包含一个明确的fields参数
FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter
答
我正在使用pod for Facebook sdk,它在iOS9.0中使用xcode 7.1。试试这段代码
I am using pod for Facebook sdk and it's work in iOS9.0 with xcode 7.1. Try this code
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();
}
}
}]