如何在iOS Social Framework中使用SLRequest获取Facebook的电子邮件参数 [英] How to get Email param of facebook using SLRequest in iOS Social Framework

查看:66
本文介绍了如何在iOS Social Framework中使用SLRequest获取Facebook的电子邮件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried the below code for getting email of the person who logged in iOS Settings Facebook. Please help me how to get email from 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"]);

            }];
        } 
    }];
}

推荐答案

您可以通过以下提到的方式获取电子邮件ID. 借助从帐户存储中获取的访问令牌来调用Facebook graph API 是的,要从Facebook获取电子邮件ID,您需要在请求访问令牌时提供电子邮件"权限,如果没有该权限,您将无法获取电子邮件参数

You can get email id by below mentioned way. Call Facebook graph API by the help of access token that you got from Account store And yes, to get email id from Facebook, you need to provide "email" permission while requesting access token, without the permission you won't be able to get email parameter

这是我的代码

NSString *FB_EncodedToken = [APP_CONSTANT.facebookToken  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

AFHTTPRequestOperationManager *opearation = [AFHTTPRequestOperationManager manager];
opearation.requestSerializer = [AFHTTPRequestSerializer serializer];
opearation.responseSerializer = [AFJSONResponseSerializer serializer];

NSString *strUrl = [NSString stringWithFormat:@"https://graph.facebook.com/me?"];
NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:FB_EncodedToken,@"access_token", nil];

[opearation GET:strUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
    DLogs(@"Description %@",responseObject);

    //Lets pasre the JSON data fetched from facebook
    [self parseUserDetail:responseObject];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    DLogs(@"Error description %@",error.description);
    self.completionHandler(error);
}];

然后解析数据

-(void)parseUserDetail:(NSDictionary *)dict
{
    FBProfileBO *profile = [[FBProfileBO alloc] init];

    profile.userFirstName = [dict objectForKey:@"first_name"];
    profile.userLastName = [dict objectForKey:@"last_name"];
    profile.userEmail = [dict objectForKey:@"email"];
    profile.userName = [dict objectForKey:@"name"];
    profile.userDOB = [dict objectForKey:@""];
    profile.facebookId = [dict objectForKey:@"id"];

    //Call back methods
    self.completionHandler(profile);
    profile = nil;
}

这篇关于如何在iOS Social Framework中使用SLRequest获取Facebook的电子邮件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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