簡體   English   中英

在iOS中使用Facebook SDK登錄時無法獲取用戶的電子郵件ID

[英]Unable to get the email id of the User when Log-in with Facebook SDK in iOS

當我使用下面的代碼獲取用戶的email-id時,我得到[null]值。 此外,我已經設置了訪問這樣的電子郵件的權限。

{
self.fbLoginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
}

同時在委托中獲取電子郵件詳細信息。

- (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"]);


             }
         }];
    }

}

請提供任何解決方案。

另外,我將FacebookSDK集成文檔用於實現鏈接: https//developers.facebook.com/docs/facebook-login/ios#login-button

您可以嘗試此代碼。 基於Facebook SDK 4.0版。 與舊版本相比,它在4.0中進行了修改。

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];
}

viewController.m文件中

- (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);
             }
         }];

    }

}

在viewdidload方法中調用此函數fetchUserInfo

[self fetchUserInfo];

通過調用此方法,您將在登錄后獲得訪問令牌,並且您希望明確指定有關該電子郵件的權限。 通過此方法,您可以獲取用戶的電子郵件。 請享用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM