gpt4 book ai didi

ios - 使用 Social Framework 从 Facebook 获取基本信息(id,first_name) - 附上源代码和屏幕截图

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:05 29 4
gpt4 key购买 nike

对于稍后的文字游戏,我尝试使用社交框架获取有关玩家的非常基本的信息:

  • 编号
  • 名字
  • 性别
  • 地点

(即没有像 email 这样敏感的东西,而且我不使用 Facebook iOS SDK)。

所以在 Xcode 5.0.2 中,我为 iPhone 创建了一个空白的单 View 应用程序,并将 Social.framework 添加到 Build phases 选项卡。

然后我将以下代码添加到 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

并创建a new Facebook app为此,我将 de.afarber.MyFacebook 指定为 iOS Bundle ID:

enter image description here

最后,在 Apple iTunes Connect 上,我创建了一个新的应用程序 ID,然后创建了一个新的应用程序(带有一个虚拟图标,甚至附有 2 个 iPhone 屏幕截图):

enter image description here

enter image description here

我的 Xcode Info 选项卡和源代码的其余部分没有变化:

enter image description here

更新 2:

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

另外,我有another Facebook game ,适用于 Adob​​e AIR 桌面和移动应用程序(自 1-2 年以来)(但现在我尝试学习 native iOS 编程)并且我尝试了它的 ID 262571703638 但没有成功。

并且我已经按照 Mohit10 的建议将 FacebookAppID 添加到 MyFacebook-Info.plist (在我看来,它只需要 Facebook SDK,而我尝试使用 Social Framework - 但它不会伤害...)

现在我得到调试器输出:

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、名字、性别、位置(以及是否真的需要 basic_info)...

最佳答案

有了有用的评论(谢谢),我终于能够使用以下代码获取一些信息:

#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

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

关于ios - 使用 Social Framework 从 Facebook 获取基本信息(id,first_name) - 附上源代码和屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829767/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com