简体   繁体   English

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

[英]How to get Email param of facebook using SLRequest in iOS Social Framework

I tried the below code for getting email of the person who logged in iOS Settings Facebook. 我尝试使用以下代码获取登录iOS设置Facebook的人的电子邮件。 Please help me how to get email from SLRequest. 请帮我如何从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"]);

            }];
        } 
    }];
}

Here is the code tested on iOS 8 that returns email. 以下是在iOS 8上测试的返回电子邮件的代码。 The solution doesn't require Facebook SDK, although the system grants access to Facebook account only if a user logged in with Facebook in Settings app. 该解决方案不需要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);
                }
            }
        }];            
    }
}];

You can get email id by below mentioned way. 您可以通过下面提到的方式获取电子邮件ID。 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 通过您从帐户商店获得的访问令牌的帮助调用Facebook图形API是的,要从Facebook获取电子邮件ID,您需要在请求访问令牌时提供“电子邮件”权限,未经您的许可您将无法获得邮件参数

Here is my code 这是我的代码

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);
}];

Then parse the data 然后解析数据

-(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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM