FBLoginView收到电子邮件

时间:2013-08-13 19:21:29

标签: ios facebook

我正在使用FBLoginView来获取电子邮件,它在获取user.email时给了我编译错误。

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    [self customizeFBButton];
    NSString *name =[user.first_name stringByAppendingFormat:@" %@",user.last_name ];
    NSLog(@"Access Token %@", [[FBSession.activeSession accessTokenData] accessToken]);
    Consumer *c=[[Consumer alloc]init];
    c.FullName=name;
    c.ProfilePictureUrl=user.link;
    c.AccountType=cAccountTypeFB;
    c.Dob=user.birthday;
    c.Email=user.email;

2 个答案:

答案 0 :(得分:3)

您需要使用FBLoginView添加读取权限“email”,例如见下文。

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

答案 1 :(得分:0)

原因是FBGraphUser协议不包含email属性。