2014-12-17 6 views
3

iOS Settings Facebook에 로그인 한 사람의 이메일을 받기 위해 아래 코드를 시도했습니다. SLRequest에서 이메일을받는 방법을 알려주세요.iOS Social Framework에서 SLRequest를 사용하여 페이스 북의 이메일 param을 얻는 방법

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

      }]; 
     } 
    }]; 
} 

답변

1

아래에서 언급 한 방법으로 이메일 ID를받을 수 있습니다. 계정 저장소 에서 가져온 액세스 토큰을 사용하여 Facebook 그래프 API를 호출하십시오. 예, 페이스 북에서 이메일 ID를 얻으려면 권한이없는 액세스 토큰을 요청하는 동안 "이메일"권한을 제공해야합니다. 이메일 매개 변수를

를 얻기 위해 여기에 여기에 이메일을 반환 아이폰 OS 8 테스트 코드가

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

감사합니다. –

+0

당신은 환영합니다, 나는 당신을 도왔다 니 기쁘다. – Janmenjaya

2

을 구문 분석 내 코드입니다. 이 솔루션은 사용자가 설정 앱에서 Facebook으로 로그인 한 경우에만 시스템이 Facebook 계정에 대한 액세스 권한을 부여하기는하지만 Facebook SDK가 필요하지 않습니다.

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