本文介绍了Facebook iOS 6 - 获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用内置的iOS 6 Facebook整合来获取用户的基本信息(电子邮件地址,生日等)?我看到的所有文档/示例都使用iOS 6集成,只需打开一个 SLComposeViewController

Is it possible to use the built-in iOS 6 Facebook integration to get the user's basic info (email address, birthday, etc)? All of the documentation/examples I have seen use the iOS 6 integration to simply open an SLComposeViewController.

谢谢为您的时间。

推荐答案

请查看我的示例项目。它允许您将视频上传到Facebook,但它还包括一种获取信息的方法,您应该查看文件 ViewController.m ,该文件中的Native在选项卡控制器。

Please check out my sample project. It allows you to upload video to Facebook, but it also includes a method to get your info, you should look at the file ViewController.m, the one noted "Native" in the tab controller.

您需要导入 Social 帐户框架来做你想要的。您可以从 ACAccountStore 请求访问用户Facebook帐户,如果您被授予访问权限,那么您可以使用此帐户创建一个 SLRequest 与你想要的参数,这里你想要图形对象/ me。

You will need to import the Social and Accounts frameworks to do what you want. You request access to the users Facebook account from the ACAccountStore, if you are granted access, then you use this account to create an SLRequest with the parameters you want, here you want the graph object "/me".

属性:

@property (nonatomic, retain) ACAccountStore *accountStore;
@property (nonatomic, retain) ACAccount *facebookAccount;

验证:

- (IBAction)getMeButtonTapped:(id)sender {
    if(!_accountStore)
        _accountStore = [[ACAccountStore alloc] init];

    ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    [_accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                           options:@{ACFacebookAppIdKey: @"483616868329082", ACFacebookPermissionsKey: @[@"email"]}
                                        completion:^(BOOL granted, NSError *error) {
                                            if(granted){
                                                NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
                                                _facebookAccount = [accounts lastObject];
                                                NSLog(@"Success");

                                                [self me];
                                            }else{
                                                // ouch
                                                NSLog(@"Fail");
                                                NSLog(@"Error: %@", error);
                                            }
                                        }];
}

获取我:

- (void)me{
    NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];

    SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                              requestMethod:SLRequestMethodGET
                                                        URL:meurl
                                                 parameters:nil];

    merequest.account = _facebookAccount;

    [merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        NSLog(@"%@", meDataString);

    }];

}

这篇关于Facebook iOS 6 - 获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:22