2013-05-08 40 views
2

我想從我的應用程序發送邀請Facebook好友,就像在Instagram應用程序中一樣。如何在iOS 6中獲取Facebook好友?

但我找不到任何好的教程。

我能夠獲得名稱&朋友的ID,但我無法獲取配置文件圖像,名稱等的URL。爲朋友。

我的代碼如下:

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

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
              requestMethod:SLRequestMethodGET 
                 URL:requestURL 
               parameters:nil]; 
    request.account = self.facebookAccount; 

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

     if(!error) 
     { 
      list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      NSLog(@"Dictionary contains data: %@", list); 
      if([list objectForKey:@"error"]!=nil) 
      { 
       [self attemptRenewCredentials]; 
      } 
      dispatch_async(dispatch_get_main_queue(),^{ 
       nameLabel.text = [list objectForKey:@"username"]; 
      }); 
     } 
     else{ 
      //handle error gracefully 
      NSLog(@"error from get%@",error); 
      //attempt to revalidate credentials 
     } 

    }]; 

那麼,如何才能做到這一點?

+1

你有什麼試過? http://developers.facebook.com/docs/tutorials/ios-sdk-games/requests/ – Buntylm 2013-05-08 08:51:30

回答

0

你應該包括這在參數

[ NSMutableDictionary dictionaryWithObjectsAndKeys:@"picture,id,name,link,gender,last_name,first_name",@"fields",nil] 
+0

但我應該在哪裏寫這行? – user2357044 2013-05-08 10:49:52

+0

「圖片,身份證,名稱,鏈接,性別,姓氏,名字」在參數 – Phantomcho 2013-05-08 11:01:24

+0

你的意思是在網址? – user2357044 2013-05-08 11:17:37

9

這是代碼,它獲取好友列表

ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
__block ACAccount *facebookAccount = nil; 

ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

// Specify App ID and permissions 
NSDictionary *options = @{ 
ACFacebookAppIdKey: @"add_here_your_project_FB_ID", 
ACFacebookPermissionsKey: @[@"publish_stream", @"publish_actions",@"read_friendlists"], 
ACFacebookAudienceKey: ACFacebookAudienceFriends 
}; 

[accountStore requestAccessToAccountsWithType:facebookAccountType 
             options:options completion:^(BOOL granted, NSError *error) 
{ 
    if (granted) 
    { 
     NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType]; 

     facebookAccount = [accounts lastObject]; 
    } 
    else { 

     NSLog(@"error.localizedDescription======= %@", error.localizedDescription); 
    } 

}]; 

NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType]; 
facebookAccount = [accounts lastObject]; 

//NSString *acessToken = [NSString stringWithFormat:@"%@",facebookAccount.credential.oauthToken]; 
//NSDictionary *parameters = @{@"access_token": acessToken}; 

NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name,link,gender,last_name,first_name,username",@"fields", nil]; 

NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends"]; 
SLRequest *feedRequest = [SLRequest 
          requestForServiceType:SLServiceTypeFacebook 
          requestMethod:SLRequestMethodGET 
          URL:feedURL 
          parameters:param]; 
feedRequest.account = facebookAccount; 
[feedRequest performRequestWithHandler:^(NSData *responseData, 
             NSHTTPURLResponse *urlResponse, NSError *error) 
{ 
    if(!error) 
    { 
     id json =[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 

     NSLog(@"Dictionary contains data: %@", json); 
     if([json objectForKey:@"error"]!=nil) 
     { 
      //[self attemptRenewCredentials]; 
     } 
     dispatch_async(dispatch_get_main_queue(),^{ 
      //nameLabel.text = [json objectForKey:@"username"]; 
     }); 
    } 
    else{ 
     //handle error gracefully 
     NSLog(@"error from get%@",error); 
     //attempt to revalidate credentials 
    } 
}]; 

我希望它可以幫助你。謝謝

+0

是的,我alerady獲取朋友列表,但我無法得到他們的名字,個人資料圖片等。 – user2357044 2013-05-08 11:22:02

+0

請嘗試我編輯的代碼,並讓我知道它是否工作。謝謝 – chandan 2013-05-08 12:07:24

+0

感謝您的回覆!但我無法獲得pic_square。 – user2357044 2013-05-08 12:44:39