2

我想从我的应用程序中发送邀请 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
        }

    }];

那么,我该如何实现呢?

4

2 回答 2

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

我希望它对你有帮助。谢谢

于 2013-05-08T11:01:37.193 回答
0

您应该将其包含在参数中

[ NSMutableDictionary dictionaryWithObjectsAndKeys:@"picture,id,name,link,gender,last_name,first_name",@"fields",nil]
于 2013-05-08T10:32:56.180 回答