Ios 使用社交框架从Facebook获取基本信息(id、名字)-源代码和附带的屏幕截图
对于稍后的文字游戏,我尝试使用社交框架获取玩家的基本信息:Ios 使用社交框架从Facebook获取基本信息(id、名字)-源代码和附带的屏幕截图,ios,iphone,facebook,facebook-ios-sdk,social-framework,Ios,Iphone,Facebook,Facebook Ios Sdk,Social Framework,对于稍后的文字游戏,我尝试使用社交框架获取玩家的基本信息: 身份证 名字 性别 位置 (也就是说,没有什么像电子邮件那样敏感,我也不使用Facebook iOS SDK) 因此,在Xcode 5.0.2中,我为iPhone创建了一个空白的单视图应用程序,并将Social.framework添加到构建阶段选项卡中 然后,我将以下代码添加到 我只需要弄清楚,现在如何获取id、名字、性别、位置(如果确实需要basic\u info)。您的代码有一些问题 ACAccountStore*accoun
- 身份证
- 名字
- 性别
- 位置
电子邮件那样敏感,我也不使用Facebook iOS SDK)
因此,在Xcode 5.0.2中,我为iPhone创建了一个空白的单视图应用程序,并将Social.framework
添加到构建阶段选项卡中
然后,我将以下代码添加到
我只需要弄清楚,现在如何获取id、名字、性别、位置(如果确实需要basic\u info
)。您的代码有一些问题
ACAccountStore*accountStore
超出范围,它应该是一个实例变量李>
- 您不需要用于发布的
ACFacebookAudienceKey
- 您不需要
ACFacebookPermissionsKey
,因为您需要的属性是
通过这些修复,您的代码仍然不适用于我,尽管它适用于我的应用程序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}
看起来你的应用程序的Facebook配置有问题
我能看到的唯一区别是,我的应用程序是沙盒的,我使用我的开发者帐户登录。祝你好运。由于你在.plist文件中没有做任何更改,你需要在.plist文件中添加FacebookAppID。因此,我认为这将有助于您获得用户详细信息,您也可以从您的设置中集成这些信息。通过这些有用的注释(谢谢),我终于能够通过以下代码获取一些信息:
#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
如果您有任何改进建议,我们非常欢迎。答案很简单
在viewDidLoad()中,使用:
//////请注意,您需要的参数被添加为字典
///请参阅下面的完整列表
////
谢谢,但是Facebook iOS SDK的FacebookAppID
(用于FBSession
)不是吗?当我尝试使用Apple Social Framework时…谢谢你,我已经按照你的建议更新了源代码。。。并且还尝试了另一个(已经在运行的)Facebook Id应用程序,但错误仍然是一样的:基本访问被拒绝(null)
。明天我将在我的问题上设定500英镑的奖金。你是否更新了BundleID以匹配工作的Facebook应用程序ID?删除应用程序并在执行此操作之前进行干净的重建也不会造成任何伤害,因为Xcode可能会混淆。不确定这是否与您看到的错误完全相关,但您请求的权限(id、姓名、性别、位置)实际上不是有效的FB权限。有关有效权限的列表,请参阅我使用的字符串与PHP版本(我的其他应用程序)中的字符串相同,因此我想知道您的字符串是什么意思?我不确定您的其他应用程序的功能,我只知道“id”和“first_name”不是有效的facebook权限,不应传递给ACFacebookPermissionsKey。“基本信息”、“用户生日”等都是权限。您正在设置的参数看起来就像您实际发出用户请求时所需的字段(即&fields=id、性别、名字、位置)。您是对的,也许我应该尝试一个SLRequest
w/o请求权限。。。
"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}
#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
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");
}
}];
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];
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 {
}
}];
}