【问题标题】:How to fetch Facebook friends in ios 6?如何在 iOS 6 中获取 Facebook 好友?
【发布时间】:2013-05-08 08:47:25
【问题描述】:

我想从我的应用程序中发送邀请 Facebook 朋友,就像在 Instagram 应用程序中一样。

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

我能够获取朋友的姓名和 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
        }

    }];

那么,我该如何实现呢?

【问题讨论】:

标签: iphone ios objective-c xcode facebook


【解决方案1】:

这是获取好友列表的代码

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

希望对你有帮助。谢谢

【讨论】:

  • 是的,我已经获取了好友列表,但我无法获取他们的姓名、头像等。
  • 请尝试我编辑的代码,让我知道它是否有效。谢谢
  • 感谢您的回复!但我无法获得 pic_square。
  • 你能得到朋友的其他详细信息吗?
  • 我只能获取 id 和 name。我认为查询不起作用,因为当我只尝试使用 url 时,我得到了相同的结果。
【解决方案2】:

你应该在参数中包含这个

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

【讨论】:

  • 但是我应该在哪里写这行??
  • 参数中的“图片、id、姓名、链接、性别、姓氏、名字”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多