gpt4 book ai didi

ios - 如何在 iOS 社交框架中使用 SLRequest 获取 facebook 的电子邮件参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:21 25 4
gpt4 key购买 nike

我尝试了以下代码来获取登录 iOS 设置 Facebook 的人的电子邮件。请帮助我如何从 SLRequest 获取电子邮件。

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

}];
}
}];
}

最佳答案

这是在 iOS 8 上测试的返回电子邮件的代码。该解决方案不需要 Facebook SDK,尽管只有当用户在“设置”应用中使用 Facebook 登录时,系统才会授予对 Facebook 帐户的访问权限。

// Required includes
@import Accounts;
@import Social;

// Getting email
ACAccountStore *theStore = [ACAccountStore new];
ACAccountType *theFBAccountType = [theStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *theOptions = @{
ACFacebookAppIdKey : @"YOUR_APP_ID",
ACFacebookPermissionsKey : @[@"email"]
};
[theStore requestAccessToAccountsWithType:theFBAccountType options:theOptions completion:^(BOOL granted, NSError *error) {
if (granted) {
ACAccount *theFBAccount = [theStore accountsWithAccountType:theFBAccountType].lastObject;

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:@"https://graph.facebook.com/me"]
parameters:@{@"fields" : @[@"email"]}];
request.account = theFBAccount;

[request performRequestWithHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil && ((NSHTTPURLResponse *)response).statusCode == 200) {
NSError *deserializationError;
NSDictionary *userData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&deserializationError];

if (userData != nil && deserializationError == nil) {
NSString *email = userData[@"email"];
NSLog(@"%@", email);
}
}
}];
}
}];

关于ios - 如何在 iOS 社交框架中使用 SLRequest 获取 facebook 的电子邮件参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27527311/

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