2012-10-03 2 views
0

SocialFramework를 통해 아래 방법을 사용하여 사용자에게 페이스 북 사용 권한을 요청하는 메시지가 표시되지만 기본 프로필 정보 (이름, 이메일, 아이디 등)를 검색하여 표시 할 수없는 것 같습니다. ..) 나는 이것을위한 간단한 방법이 있다고 상상하지만 찾을 수는 없다. 아무도 여기서 도움을 줄 수 있습니까? 감사합니다검색 facebook 프로필 정보 iOS6

-(IBAction)getInfo:(id)sender{ 

NSLog(@"FIRING"); 

ACAccountStore *_accountStore = [[ACAccountStore alloc] init]; 

ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

// We will pass this dictionary in the next method. It should contain your Facebook App ID key, 
// permissions and (optionally) the ACFacebookAudienceKey 
NSDictionary *options = @{ACFacebookAppIdKey : @"284395038337404", 
ACFacebookPermissionsKey : @[@"email"], 
ACFacebookAudienceKey:ACFacebookAudienceOnlyMe}; 

// Request access to the Facebook account. 
// The user will see an alert view when you perform this method. 
[_accountStore requestAccessToAccountsWithType:facebookAccountType 
             options:options 
            completion:^(BOOL granted, NSError *error) { 
             if (granted) 
             { 
              NSLog(@"GRANTED"); 
              // At this point we can assume that we have access to the Facebook account 
              NSArray *accounts = [_accountStore accountsWithAccountType:facebookAccountType]; 

              // Optionally save the account 
              [_accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil]; 
             } 
             else 
             { 
              NSLog(@"Failed to grant access\n%@", error); 
             } 
            }]; 

} 페이스 북이 경우에 화면 이름이나 설명으로 당신은 단지 당신이 얻고있는 계정의 아주 기본적인 정보에 액세스 할 수 있습니다 게시 한 코드로

+0

안녕하세요. 해결책이 있습니까? 나는 널 응답을 얻고있다. –

+0

실제로 아직 아래의 솔루션을 테스트/구현할 기회가 없었습니다. – Fluffhead

답변

2

...

예 : 당신이 그래프 페이스 북 API의/저 함수를 사용하여 SLRequest을해야 등 실제 이름, 이메일로보다 완벽한 정보를 얻기 위하여

ACAccount *account = [accounts lastObject]; 
NSString *username = account.username; 

.

NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 
NSString *serviceType = SLServiceTypeFacebook; 

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

[queue setName:@"Perform request"]; 
[queue addOperationWithBlock:^{ 

    SLRequest *request = [SLRequest requestForServiceType:serviceType requestMethod:SLRequestMethodGET URL:requestURL parameters:nil]; 

    [request setAccount:account]; 

    NSLog(@"Token: %@", account.credential.oauthToken); 

    [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) { 

     // Handle the response... 
     if (error) { 
      NSLog(@"Error: %@", error); 
      //Handle error 
     } 
     else { 

      NSDictionary* jsonResults = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      if (error) { 

       NSLog(@"Error: Error serializating object"); 
       //Handle error 
      } 
      else { 

        NSDictionary *errorDictionary = [jsonResults valueForKey:@"error"]; 

        if (errorDictionary) { 

         NSNumber *errorCode = [errorDictionary valueForKey:@"code"]; 

         //If we get a 190 code error, renew credentials 
         if ([errorCode isEqualToNumber:[NSNumber numberWithInt:190]]) { 

          NSLog(@"Renewing credenciales..."); 

          [self.accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){ 

           if (error) { 
            NSLog(@"Error: %@", error); 
            //Handle error 
           } 
           else { 
            if (renewResult == ACAccountCredentialRenewResultRenewed) { 
             //Try it again 
            } 
            else { 
             NSLog(@"Error renewing credenciales..."); 

             NSError *errorRenewengCredential = [[NSError alloc] initWithDomain:@"Error reneweng facebook credentials" code:[errorCode intValue] userInfo:nil]; 

             if (renewResult == ACAccountCredentialRenewResultFailed) { 
              //Handle error 
             } 
             else if (renewResult == ACAccountCredentialRenewResultRejected) { 
              //Handle error 
             } 
            } 
           } 
          }]; 
         } 
        } 
        else { 
         [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
          NSLog(@"jsonResults: %@", jsonResults); 
         }]; 
        } 
       } 
      } 
     } 
    }]; 
}]; 

이 코드는 일부 토큰이 만료 된 경우, 취급 및 인터페이스를 차단하지하기 위해 백그라운드에서 네트워크 프로세스를 실행, 오류의 가능성을 확인합니다.

당신은 jsonResults 사전에 모든 정보를 찾을 것이며, 다음과 같이 액세스 할 수 있습니다

NSString *name = [jsonResults objectForKey:@"first_name"]; 
NSString *lastName = [jsonResults objectForKey:@"last_name"]; 
NSString *email = [jsonResults objectForKey:@"email"]; 

확인 페이스 북의 문서를 자세한 내용은에서 : https://developers.facebook.com/docs/reference/api/user/

는 희망이 도움이!

+1

그것은 나를 도왔습니다 ... Thnx .. – BhushanVU