您需要使用 FBLoginView 添加读取权限“电子邮件”,例如见下文。
- (void)viewDidLoad {
[super viewDidLoad];
FBLoginView *loginview = [[FBLoginView alloc] init];
// Ask for the required permissions
loginview.readPermissions = @[@"email",
@"basic_info",
@"user_location",
@"user_birthday",
@"user_likes"];
loginview.frame = CGRectOffset(loginview.frame, 5, 5);
loginview.delegate = self;
[self.view addSubview:loginview];
[loginview sizeToFit];
}
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
// Fetch user data
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> fbUserData,
NSError *error)
{
if (!error)
{
if ([fbUserData objectForKey:@"email"])
{
NSString *FBEmail = [fbUserData objectForKey:@"email"];
if (![FBEmail isEqualToString:@""] && FBEmail != nil)
{
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
NSString *FBId = [fbUserData objectForKey:@"id"];
if (![FBId isEqualToString:@""] && FBId != nil)
{
[dict setObject:FBId forKey:@"FB_ID"];
}else
{
[dict setObject:@"" forKey:@"FB_ID"];
}
NSString *FBEmail = [fbUserData objectForKey:@"email"];
if (![FBEmail isEqualToString:@""] && FBEmail != nil)
{
[dict setObject:FBEmail forKey:@"FB_Email"];
}else
{
[dict setObject:@"" forKey:@"FB_Email"];
}
NSString *name = [fbUserData objectForKey:@"name"];
if (![name isEqualToString:@""] && name != nil)
{
[dict setObject:name forKey:@"FB_FullName"];
}else
{
[dict setObject:@"" forKey:@"FB_FullName"];
}
NSString *first_name = [fbUserData objectForKey:@"first_name"];
if (![first_name isEqualToString:@""] && first_name != nil)
{
[dict setObject:first_name forKey:@"FB_FirstName"];
}else
{
[dict setObject:@"" forKey:@"FB_FirstName"];
}
NSString *last_name = [fbUserData objectForKey:@"last_name"];
if (![last_name isEqualToString:@""] && last_name != nil)
{
[dict setObject:last_name forKey:@"FB_LastName"];
}else
{
[dict setObject:@"" forKey:@"FB_LastName"];
}
NSString *username = [fbUserData objectForKey:@"username"];
if (![username isEqualToString:@""] && username != nil)
{
[dict setObject:username forKey:@"FB_UserName"];
}else
{
[dict setObject:@"" forKey:@"FB_UserName"];
}
if ([[fbUserData objectForKey:@"gender"] isEqualToString:@"male"])
{
[dict setObject:@"Male" forKey:@"FB_Gender"];
}
else{
[dict setObject:@"Female" forKey:@"FB_Gender"];
}
NSString *birthday = [fbUserData objectForKey:@"birthday"];
NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
[format1 setDateFormat:@"MM/dd/yyyy"];
NSDate *dateConversion = [format1 dateFromString:birthday];
//[format1 release];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
birthday = [format stringFromDate:dateConversion];
if (![birthday isEqualToString:@""] && birthday != nil)
{
[dict setObject:birthday forKey:@"FB_DOB"];
}else
{
[dict setObject:@"" forKey:@"FB_DOB"];
}
//[format release];
NSDictionary *locationDict = [fbUserData objectForKey:@"location"];
if(locationDict)
{
NSString *locationStr = [locationDict objectForKey:@"name"];
if(locationStr.length > 0)
{
NSArray *locationsComponentsArr = [locationStr componentsSeparatedByString:@","];
if([locationsComponentsArr objectAtIndex:0])
{
[dict setObject:[locationsComponentsArr objectAtIndex:0] forKey:@"FB_City"];
}else
{
[dict setObject:@"" forKey:@"FB_City"];
}
if([locationsComponentsArr objectAtIndex:1])
{
[dict setObject:[locationsComponentsArr objectAtIndex:1] forKey:@"FB_Country"];
}else
{
[dict setObject:@"" forKey:@"FB_Country"];
}
}
else
{
[dict setObject:@"" forKey:@"FB_City"];
[dict setObject:@"" forKey:@"FB_Country"];
}
}else
{
[dict setObject:@"" forKey:@"FB_City"];
[dict setObject:@"" forKey:@"FB_Country"];
}
NSLog(@"FBUserDataDictInfo: %@", dict);
dict = nil;
}
}
}
}];
}