2012-11-20 136 views
14

我與電子郵件的權限打開Facebook的會議,像這樣:如何在iOS上使用Facebook SDK 3.1獲取用戶的電子郵件?

- (void)facebookOpenSession { 
    NSArray *permissions = @[@"email"]; 
    [FBSession openActiveSessionWithReadPermissions:permissions 
             allowLoginUI:YES 
            completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 
     [self sessionStateChanged:session state:state error:error]; 
    }]; 
} 

然後會話狀態的重要片斷改變的樣子:

- (void)sessionStateChanged:(FBSession *)session 
         state:(FBSessionState) state 
         error:(NSError *)error { 
    switch (state) { 
     case FBSessionStateOpen: { 
      [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 
      if (error) { 
       //error 
      } else { 
       self.myFirstNameLabel.text = user.first_name; 
       self.myLastNameLabel.text = user.last_name; 
       // self.myEmailLabel.text = @""; 
      } 
     }]; 
    } 
    ... 

如何確實提取了用戶的電子郵件?它是否在提供的變量之一中,還是必須在完成處理程序中再次調用?

在此先感謝!

回答

26

FBGraphUser沒有財產(我假設,因爲它的可選/只可如果你問的電子郵件的權限),但你還是應該能夠從字典訪問這樣的:

[user objectForKey:@"email"] 
+0

是的,這沒有竅門! – user1601540

0

你應該檢查之前喲要求正確的權限

相關問題