2

首先,我不想使用 Facebook SDK,所以我设法做到了,这是可行的,但我不知道如何检索用户的电子邮件,即使它在我的权限请求中。

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

ACAccount 上没有返回用户电子邮件的属性,因此我尝试查找是否必须通过 SLRequest 完成但找不到。

可能吗?

4

2 回答 2

5

为了从 Facebook 获取用户的电子邮件地址,您必须使用Graph API使用Social 框架查询当前用户

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);
        }
    }
}];
于 2013-10-31T22:49:13.957 回答
0

现在您需要在 GET 请求中传递参数。默认情况下只有 ID 和名称。

NSDictionary *param = @{@"fields":@"email,name"};

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                     requestMethod:SLRequestMethodGET
                                                               URL:[NSURL URLWithString:@"https://graph.facebook.com/me"]
                                                        parameters:param];
于 2015-11-18T07:36:56.830 回答