2012-10-03 59 views
0

我已成功提示用戶授予Facebook權限,使用以下方法通過SocialFramework,但似乎無法檢索並顯示基本檔案信息(名稱,電子郵件,ID等)。 ..)我想有這個簡單的方法,但無法找到它們。任何人都可以提供一些幫助嗎?由於檢索臉書檔案信息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

隨着這是Facebook在這種情況下,你已經張貼你可以訪問只有你所得到的帳戶的非常基本的信息的代碼,如屏幕名稱或說明...

一個例子:

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

爲了得到更完整的信息,真實姓名,電子郵件等,你需要使用Facebook的圖形API的/我的功能做一個SLRequest。

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