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

馋奶兔 提交于 2019-12-06 02:25:58
Janmenjaya

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

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

Here is the code tested on iOS 8 that returns email. 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.

// 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);
                }
            }
        }];            
    }
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!