【问题标题】:Retrieve facebook profile info iOS6检索 facebook 个人资料信息 iOS6
【发布时间】:2012-10-03 18:54:49
【问题描述】:

我已成功提示用户通过 SocialFramework 使用以下方法授予 facebook 权限,但似乎无法检索并显示基本个人资料信息(姓名、电子邮件、ID 等...)我想对此有简单的方法,但找不到它们。任何人都可以在这里提供一些帮助吗?谢谢

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

}

【问题讨论】:

  • 嗨..你有解决方案吗?我得到空响应。
  • 我实际上还没有机会测试/实施以下解决方案。

标签: facebook ios6 social-framework


【解决方案1】:

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

一个例子:

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

为了获得更完整的信息,如真实姓名、电子邮件等,您需要使用 graph 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/

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2014-05-02
    • 2018-08-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多