检索facebook个人资料信息iOS6

时间:2012-10-03 18:54:50

标签: facebook ios6 social-framework

我已成功提示用户授予facebook权限,使用下面的方法通过SocialFramework,但似乎无法检索,然后显示基本的个人资料信息(姓名,电子邮件,身份证等...)我想象有简单的方法,但找不到它们。有谁可以在这里提供一些帮助?感谢

-(IBAction)getInfo:(id)sender{

NSLog(@"FIRING");

ACAccountStore *_accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

// We will pass this dictionary in the next method. It should contain your Facebook App ID key,
// permissions and (optionally) the ACFacebookAudienceKey
NSDictionary *options = @{ACFacebookAppIdKey : @"284395038337404",
ACFacebookPermissionsKey : @[@"email"],
ACFacebookAudienceKey:ACFacebookAudienceOnlyMe};

// Request access to the Facebook account.
// The user will see an alert view when you perform this method.
[_accountStore requestAccessToAccountsWithType:facebookAccountType
                                       options:options
                                    completion:^(BOOL granted, NSError *error) {
                                        if (granted)
                                        {
                                            NSLog(@"GRANTED");
                                            // At this point we can assume that we have access to the Facebook account
                                            NSArray *accounts = [_accountStore accountsWithAccountType:facebookAccountType];

                                            // Optionally save the account
                                            [_accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];
                                        }
                                        else
                                        {
                                            NSLog(@"Failed to grant access\n%@", error);
                                        }
                                    }];

}

1 个答案:

答案 0 :(得分:2)

使用您发布的代码,您只能访问您获得的帐户的非常基本的信息,例如在这种情况下作为Facebook的屏幕名称或说明...

一个例子:

ACAccount *account = [accounts lastObject];
NSString *username = account.username;

为了以真实姓名,电子邮件等方式获取更完整的信息,您需要使用图表facebook api的/ me功能进行SLRequest。

NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
NSString *serviceType = SLServiceTypeFacebook;

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[queue setName:@"Perform request"];
[queue addOperationWithBlock:^{

    SLRequest *request = [SLRequest requestForServiceType:serviceType requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];

    [request setAccount:account];

    NSLog(@"Token: %@", account.credential.oauthToken);

    [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {

        // Handle the response...
        if (error) {
            NSLog(@"Error: %@", error);
            //Handle error
        }
        else {

            NSDictionary* jsonResults = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

            if (error) {

                NSLog(@"Error: Error serializating object");
                //Handle error
            }
            else {

                    NSDictionary *errorDictionary = [jsonResults valueForKey:@"error"];

                    if (errorDictionary) {

                        NSNumber *errorCode = [errorDictionary valueForKey:@"code"];

                        //If we get a 190 code error, renew credentials
                        if ([errorCode isEqualToNumber:[NSNumber numberWithInt:190]]) {

                            NSLog(@"Renewing credenciales...");

                            [self.accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){

                                if (error) {
                                    NSLog(@"Error: %@", error);
                                    //Handle error
                                }
                                else {
                                    if (renewResult == ACAccountCredentialRenewResultRenewed) {
                                        //Try it again
                                    }
                                    else {
                                        NSLog(@"Error renewing credenciales...");

                                        NSError *errorRenewengCredential = [[NSError alloc] initWithDomain:@"Error reneweng facebook credentials" code:[errorCode intValue] userInfo:nil];

                                        if (renewResult == ACAccountCredentialRenewResultFailed) {
                                            //Handle error
                                        }
                                        else if (renewResult == ACAccountCredentialRenewResultRejected) {
                                            //Handle error
                                        }
                                    }
                                }
                            }];
                        }
                    }
                    else {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"jsonResults: %@", jsonResults);
                        }];
                    }
                }
            }
        }
    }];
}];

此代码还检查错误的可能性,如果令牌已过期则进行一些处理并在后台运行网络进程以便不阻止接口。

您可以在jsonResults字典中找到所有信息,您可以按如下方式访问:

NSString *name = [jsonResults objectForKey:@"first_name"];
NSString *lastName = [jsonResults objectForKey:@"last_name"];
NSString *email = [jsonResults objectForKey:@"email"];

查看facebook文档以获取更多信息: https://developers.facebook.com/docs/reference/api/user/

希望它有所帮助!