通过使用社交框架 – 源代码和截图,从Facebook获取基本信息(id,first_name)

对于后来的文字游戏,我试图通过使用Social框架获取有关玩家的基本信息:

  • ID
  • 名字
  • 性别
  • 位置

(即没有像email敏感,我不使用Facebook的iOS SDK)。

所以在Xcode 5.0.2中,我为iPhone创build了一个空白的单一视图应用程序,并将Social.framework添加到构build阶段选项卡。

然后我将下面的代码添加到ViewController.m中 :

 #import "ViewController.h" #import <Social/Social.h> #import <Accounts/Accounts.h> #define FB_APP_ID @"432298283565593" //#define FB_APP_ID @"262571703638" @interface ViewController () @property (strong, nonatomic) ACAccountStore *accountStore; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"Facebook is available: %d", [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]); [self getMyDetails]; } - (void) getMyDetails { if (!_accountStore) { _accountStore = [[ACAccountStore alloc] init]; } ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook]; NSDictionary *options = @{ ACFacebookAppIdKey: FB_APP_ID, ACFacebookPermissionsKey: @[@"basic_info"]}; [_accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) { if (granted) { NSLog(@"Basic access granted"); } else { NSLog(@"Basic access denied %@", error); } }]; } @end 

并创build一个新的Facebook应用程序 ,我指定de.afarber.MyFacebook作为iOS Bundle ID

在这里输入图像说明

最后,在Apple iTunes Connect中,我创build一个新的App ID,然后创build一个新的应用程序(带有一个虚拟图标,甚至附加2个iPhone截图):

在这里输入图像说明

在这里输入图像说明

我的Xcode 信息选项卡和其他源代码保持不变:

在这里输入图像说明

更新2:

我已经更新了有用评论build议的源代码,谢谢。

另外,我还有另外一款Facebook游戏 ,它适用于Adobe AIR桌面和移动应用程序(但是现在我尝试学习原生iOS编程),效果很好(从1 – 2年),而且我已经试过它的ID 262571703638但没有成功。

我已经按照MyFacebook-Info.plistbuild议,将FacebookAppID添加到了MyFacebook-Info.plist (在我看来,这只是Facebook SDK所需要的,而我尝试使用社交框架 – 但它不会伤害…)

现在我得到debugging器输出:

 2013-12-31 11:08:07.584 MyFacebook[3009:70b] Facebook is available: 1 2013-12-31 11:08:07.964 MyFacebook[3009:3903] Basic access granted 

我只需要弄清楚,现在如何获取id,first_name,gender,location(如果真的需要basic_info )…

你的代码有几个错误。

  • ACAccountStore *accountStore超出范围,它应该是一个实例variables。
  • 你不需要ACFacebookAudienceKey ,这是发布。
  • 您不需要ACFacebookPermissionsKey因为您想要的属性默认是可用的

有了这些修复程序,您的代码仍然不适用于我,尽pipe它适用于我的应用程序ID。 我得到以下错误:

 "The Facebook server could not fulfill this access request: Invalid application 432298283565593" UserInfo=0x1d5ab670 {NSLocalizedDescription=The Facebook server could not fulfill this access request: Invalid application 432298283565593} 

看起来你的应用的Facebookconfiguration有问题。

我可以看到唯一的区别是我的应用程序是沙箱,我使用我的开发人员帐户login。 祝你好运。

随着有用的意见(谢谢),我终于能够获取一些信息与下面的代码:

 #import "ViewController.h" #import <Social/Social.h> #import <Accounts/Accounts.h> #define FB_APP_ID @"432298283565593" //#define FB_APP_ID @"262571703638" @interface ViewController () @property (strong, nonatomic) ACAccount *facebookAccount; @property (strong, nonatomic) ACAccountType *facebookAccountType; @property (strong, nonatomic) ACAccountStore *accountStore; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; if (NO == [SLComposeViewController isAvailableForServiceType: SLServiceTypeFacebook]) { [self showAlert:@"There are no Facebook accounts configured. Please add or create a Facebook account in Settings."]; return; } [self getMyDetails]; } - (void) getMyDetails { if (! _accountStore) { _accountStore = [[ACAccountStore alloc] init]; } if (! _facebookAccountType) { _facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; } NSDictionary *options = @{ ACFacebookAppIdKey: FB_APP_ID }; [_accountStore requestAccessToAccountsWithType: _facebookAccountType options: options completion: ^(BOOL granted, NSError *error) { if (granted) { NSArray *accounts = [_accountStore accountsWithAccountType:_facebookAccountType]; _facebookAccount = [accounts lastObject]; NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"]; SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:nil]; request.account = _facebookAccount; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; NSLog(@"id: %@", responseDictionary[@"id"]); NSLog(@"first_name: %@", responseDictionary[@"first_name"]); NSLog(@"last_name: %@", responseDictionary[@"last_name"]); NSLog(@"gender: %@", responseDictionary[@"gender"]); NSLog(@"city: %@", responseDictionary[@"location"][@"name"]); }]; } else { [self showAlert:@"Facebook access for this app has been denied. Please edit Facebook permissions in Settings."]; } }]; } - (void) showAlert:(NSString*) msg { dispatch_async(dispatch_get_main_queue(), ^(void) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"WARNING" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; }); } @end 

这将打印我的数据:

 id: 597287941 first_name: Alexander last_name: Farber gender: male city: Bochum, Germany 

如果您有任何改进build议,非常欢迎。

由于您尚未对.plist文件进行任何更改,因此您需要将您的FacebookAppID添加到.plist文件中。 从这一点上,我认为这将有助于你获得用户的详细信息,你也可以从你的设置中整合它。 在这里输入图像说明

答案很简单

在viewDidLoad()中使用:

 accountStore= [[ACAccountStore alloc]init]; facebookAccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSDictionary *options= @{ ACFacebookAudienceKey: ACFacebookAudienceEveryone, ACFacebookAppIdKey: @"<YOUR FACEBOOK APP ID>", ACFacebookPermissionsKey: @[@"public_profile"] }; [accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) { if (granted) { NSLog(@"Permission has been granted to the app"); NSArray *accounts= [accountStore accountsWithAccountType:facebookAccountType]; facebookAccount= [accounts firstObject]; [self performSelectorOnMainThread:@selector(facebookProfile) withObject:nil waitUntilDone:NO]; } else { NSLog(@"Permission denied to the app"); } }]; 

/////和函数 – (void)facebookProfile {

 NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 

//////注意你需要的参数被添加为字典///请参考下面的完整列表//// https://developers.facebook.com/docs/graph-api/reference/user

 NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name",@"fields", nil]; SLRequest *profileInfoRequest= [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:param]; profileInfoRequest.account= facebookAccount; [profileInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSLog(@"Facebook status code is : %ld", (long)[urlResponse statusCode]); if ([urlResponse statusCode]==200) { NSDictionary *dictionaryData= [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; } else { } }]; }