Facebook iOS 6 - 获取用户信息 [英] Facebook iOS 6 - get user info

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

问题描述

是否可以使用内置的 iOS 6 Facebook 集成来获取用户的基本信息(电子邮件地址、生日等)?我见过的所有文档/示例都使用 iOS 6 集成来简单地打开一个 SLComposeViewController.

感谢您的宝贵时间.

解决方案

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

https://bitbucket.org/danielphillips/fb-video-upload

您需要导入 SocialAccounts 框架来执行您想要的操作.您从 ACAccountStore 请求访问用户 Facebook 帐户,如果您被授予访问权限,那么您可以使用此帐户创建一个带有您想要的参数的 SLRequest,在这里您想要图对象/me".

属性:

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

验证:

- (IBAction)getMeButtonTapped:(id)sender {如果(!_accountStore)_accountStore = [[ACAccountStore 分配] init];ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];[_accountStore requestAccessToAccountsWithType:facebookTypeAccount选项:@{ACFacebookAppIdKey:@"483616868329082",ACFacebookPermissionsKey:@[@"email"]}完成:^(授予布尔值,NSError *错误){如果(授予){NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];_facebookAccount = [accounts lastObject];NSLog(@"成功");[我自己];}别的{//哎哟NSLog(@"失败");NSLog(@"错误:%@", 错误);}}];}

获取我":

- (void)me{NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook请求方法:SLRequestMethodGET网址:meurl参数:无];merequest.account = _facebookAccount;[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];NSLog(@"%@", meDataString);}];}

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.

Thank you for your time.

解决方案

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.

https://bitbucket.org/danielphillips/fb-video-upload

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".

Properties:

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

Authenticate:

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

Get "me":

- (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 - 获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆