Reputation: 53
I am getting [null] value when I am fetching the user's email-id using below code. Also, I already set the permission to access the email like this.
{
self.fbLoginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
}
Also Fetching the email details in delegate.
- (void)loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult: (FBSDKLoginManagerLoginResult *)result
error: (NSError *)error
{
if ([FBSDKAccessToken currentAccessToken])
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@", result);
NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]);
}
}];
}
}
Please provide any solution in this.
Also, I refer the FacebookSDK integration docs for the implementation link:https://developers.facebook.com/docs/facebook-login/ios#login-button
Upvotes: 5
Views: 3581
Reputation: 1227
You can try this code. Based on Facebook SDK version 4.0. It is modified in 4.0 when compared with the older version.
In App delegate.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
In viewController.m
file
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:@"email"])
{
NSLog(@"result is:%@",result);
[self fetchUserInfo];
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"resultis:%@",result);
}
else
{
NSLog(@"Error %@",error);
}
}];
}
}
In viewdidload method call this function fetchUserInfo
[self fetchUserInfo];
By calling this method you will get the access token after login and you want to clearly specify in permission about the email. by this method you can able to fetch the email of the user. enjoy
Upvotes: 14