2012-04-25 66 views
7

我想要訪問一些用戶的信息,包括電子郵件。爲此,我使用圖形API來調用這個方法:Facebook的iOS SDK沒有返回用戶的電子郵件

[facebook requestWithGraphPath:@"me" andDelegate:self]; 

它返回的用戶信息,而電子郵件信息(生日也丟失)。我設置了所有權限並使用多個帳戶(其中包含電子郵件和生日作爲公共信息)進行測試,並接受了所有對話的請求。

這裏是我的init:

if(!facebook) { 
    facebook = [[Facebook alloc] initWithAppId:[self _APP_KEY] andDelegate:self]; 
    NSArray *array = [NSArray arrayWithObjects: @"email", @"user_birthday", @"publish_stream", @"offline_access", nil]; 
    [self setPermissions:array]; 
} 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) { 
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 
} 

if (![facebook isSessionValid]) { 
    [facebook authorize:permissions]; 
} 
else { 
    [self getUserInfo]; 
} 

登錄回調:

- (void)fbDidLogin { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 

    [self getUserInfo]; 
} 

的getUserInfo方法:

- (void) getUserInfo { 
    [facebook requestWithGraphPath:@"me" andDelegate:self]; 
} 

請求回調:

- (void)request:(FBRequest *)request didLoad:(id)result { 

    if ([result isKindOfClass:[NSDictionary class]]) { 
     NSDictionary* json = result; 
     NSLog(@"-> %@", json); 
    } 
} 

結果如下:

-> { 
    "first_name" = EDITED (my name); 
    gender = male; 
    hometown =  { 
     id = 111072692249998; 
     name = "Porto Alegre"; 
    }; 
    id = 653099452; 
    languages =  (
       { 
      id = 104034306299811; 
      name = "Brazilian Portuguese"; 
     }, 
       { 
      id = 106059522759137; 
      name = English; 
     } 
    ); 
    "last_name" = EDITED (my last name); 
    link = EDITED (my profile); 
    locale = "en_US"; 
    location =  { 
     id = 112047398814697; 
     name = "S\U00e3o Paulo, Brazil"; 
    }; 
    name = EDITED (my name); 
    timezone = "-3"; 
    "updated_time" = "2012-04-25T13:36:51+0000"; 
    verified = 1; 
} 

正如你所看到的,沒有關於該用戶的電子郵件信息。我錯過了一些步驟?任何意識?

在此先感謝

+1

以上答案都不對工作的所有Facebook賬戶?或者他們?爲了其他用戶的利益,您應該選擇正確的答案。 – Dobler 2012-08-11 04:36:10

回答

7

嘗試使用此,它的作品在我的情況

facebook = [[Facebook alloc] initWithAppId:[self _APP_KEY] andDelegate:self]; [facebook授權:[NSArray arrayWithObjects:@「email」,nil]];

1

你可以得到用戶的電子郵件這樣的..後fbDidlogin解僱調用此方法

- (void)fetchUserDetails { 
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
            @"SELECT uid, name, pic, email FROM user WHERE uid=me()", @"query",nil]; 

    [fb requestWithMethodName:@"fql.query" 
            andParams:params 
           andHttpMethod:@"POST" 
            andDelegate:self]; 
} 

,並在request:(FBRequest *)request didLoad,你會做這樣的事情:

- (void)request:(FBRequest *)request didLoad:(id)result 
{ 

    if([request.url rangeOfString:@"fql.query"].location !=NSNotFound) 
    { 
     if ([result isKindOfClass:[NSArray class]] && [result count]>0) { 
      result = [result objectAtIndex:0]; 
     } 

     if ([result objectForKey:@"email"]) { 

      NSLog(@"%@",[result objectForKey:@"email"]); 
      } 
     } 
} 

享受:)

0

您需要通過請求允許用戶輸入以下信息:

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { 
NSArray *permissions = [[NSArray alloc] initWithObjects: 
         @"user_birthday", 
         nil]; 
return [FBSession openActiveSessionWithPermissions:permissions 
             allowLoginUI:allowLoginUI 
           completionHandler:^(FBSession *session, 
                FBSessionState state, 
                NSError *error) { 
            [self sessionStateChanged:session 
                 state:state 
                 error:error]; 
           }]; 

}

這裏有您需要的所有信息,只要按照這個教程

首先,這一個:Facebook Login

然後,這一個:Fetch user data

3

試試這個 permissions = [[NSArray alloc] initWithObjects:@「offline_access」,@「email」,nil];

1

1)給獲得電子郵件和信息等方面的權限...

// You must ALWAYS ask for public_profile permissions when opening a session 
       [FBSession openActiveSessionWithReadPermissions:@[@"public_profile",@"user_videos",@"email"] 
                allowLoginUI:YES 
               completionHandler: 
       ^(FBSession *session, FBSessionState state, NSError *error) 
       { 
        // Retrieve the app delegate 
        AppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
        // Call the app delegate's sessionStateChanged:state:error method to handle session state changes 
        [appDelegate sessionStateChanged:session state:state error:error]; 
       }]; 

2),以獲得您想要的電子郵件...

if (FBSession.activeSession.isOpen) 
    { 
     [[FBRequest requestForMe] startWithCompletionHandler: 
     ^(FBRequestConnection *connection, 
      NSDictionary<FBGraphUser> *user, 
      NSError *error) 
     { 
      if (!error) 
      { 
       NSString *userEmail = [user objectForKey:@"email"]; 
      } 
      [tblSettingVw reloadData]; 
     }]; 
    } 

注:用戶詞典將返回以下內容

{ 
    "first_name" =; 
    gender =; 
    id =; 
    "last_name" =; 
    link = ""; 
    locale = ""; 
    name = ""; 
    timezone = ""; 
    "updated_time" = ""; 
    verified = ; 
} 
1

它們通過電話號碼,而不是電子郵件驗證將獲得NULL[user objectForKey:@"email"]

相關問題