iosobjective-cfacebookpermissionsacaccount

Getting email using ACAccount for Facebook iOS 7


First of all, I don't want to use Facebook SDK, so I managed to do this, which is working, but I don't know how to retrieve user's email, even though it is on my permissions request.

ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:accountTypeName];

NSDictionary *options;

if ( [accountTypeName isEqualToString:ACAccountTypeIdentifierFacebook]){
    options = @{
                ACFacebookAppIdKey: @"601517946570890"
                ,ACFacebookPermissionsKey: @[@"email"]
                };
}

[account requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error)
 {
     if (granted == YES)
     {
         NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

         if ([arrayOfAccounts count] > 0)
         {
             _account = [arrayOfAccounts lastObject];

             NSDictionary *dict = @{
                                    @"name": [_account userFullName] == nil ? [_account username] : [_account userFullName],
                                    @"account_id": [_account identifier],
                                    @"email": @"??"
                                    };

             NSLog(@"account info: %@",dict);
         }
     }
 }];

there is no property on ACAccount that returns user e-mail, so I tried to find if it had to be done via SLRequest and couldn't find it.

Is it possible?


Solution

  • In order to get the user's email address from Facebook, you have to use the Graph API to query the current user using the Social framework:

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                         requestMethod:SLRequestMethodGET
                                                   URL:[NSURL URLWithString:@"https://graph.facebook.com/me"]
                                            parameters:nil];
    request.account = _account; // This is the _account from your code
    [request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error == nil && ((NSHTTPURLResponse *)response).statusCode == 200) {
            NSError *deserializationError;
            NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&deserializationError];
    
            if (userData != nil && deserializationError == nil) {
                NSString *email = userData[@"email"];
                NSLog(@"%@", email);
            }
        }
    }];