簡體   English   中英

如何在iOS社交框架中使用SLRequest獲取Facebook的電子郵件參數

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

我嘗試使用以下代碼獲取登錄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);
                }
            }
        }];            
    }
}];

您可以通過下面提到的方式獲取電子郵件ID。 通過您從帳戶商店獲得的訪問令牌的幫助調用Facebook圖形API是的,要從Facebook獲取電子郵件ID,您需要在請求訪問令牌時提供“電子郵件”權限,未經您的許可您將無法獲得郵件參數

這是我的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM