在ios 6.0中使用SLRequest获取Facebook好友列表

时间:2012-12-24 04:34:06

标签: objective-c facebook ios6 social

我将ios 6.0中的facebook集成为

How to integrate Facebook in iOS 6 using SLRequest?

但是我无法使用SLRequest获取facebook好友列表。任何人都可以知道如何做到这一点?我想在我的游戏中实现“与朋友一起玩”这类功能。

由于

2 个答案:

答案 0 :(得分:11)

我认为它应该如何运作。

以下代码对我来说很好。

 NSArray *accounts = [accountStore
                               accountsWithAccountType:facebookAccountType];
          ACAccount *facebookAccount = [accounts lastObject];
          NSString *acessToken = [NSString stringWithFormat:@"%@",facebookAccount.credential.oauthToken];
          NSDictionary *parameters = @{@"access_token": acessToken};
          NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends"];
          SLRequest *feedRequest = [SLRequest
                                    requestForServiceType:SLServiceTypeFacebook
                                    requestMethod:SLRequestMethodGET
                                    URL:feedURL
                                    parameters:parameters];
          feedRequest.account = facebookAccount;
          [feedRequest performRequestWithHandler:^(NSData *responseData,
                                                   NSHTTPURLResponse *urlResponse, NSError *error)
           {
               NSLog(@"%@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
           }];
      }
      else
      {
          // Handle Failure
      }

答案 1 :(得分:4)

-(void)getFacebookFriendsList
{
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        ACAccountStore *store = [[ACAccountStore alloc] init];

        ACAccountType *facebookTypeAccount = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
        //Replace with your Facebook.com app ID
        NSDictionary *options = @{ACFacebookAppIdKey:kFacebookAppID, ACFacebookPermissionsKey:@[@"email", @"read_friendlists"],
                                  ACFacebookAudienceKey:ACFacebookAudienceFriends};

        [store requestAccessToAccountsWithType:facebookTypeAccount
                                       options:options
                                    completion:^(BOOL granted, NSError *error){

                                        if(granted)
                                        {
                                            NSArray *accounts = [store accountsWithAccountType:facebookTypeAccount];
                                            ACAccount *facebookAccount = [accounts lastObject];

                                            NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];
                                            SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                                                      requestMethod:SLRequestMethodGET
                                                                                                URL:meurl
                                                                                         parameters:nil];
                                            merequest.account = facebookAccount;

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

                                            SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                                                    requestMethod:SLRequestMethodGET
                                                                                              URL:requestURL parameters:@{@"fields":@"id, name, email, picture, first_name, last_name, gender, installed"}];
                                            request.account = facebookAccount;
                                            [request performRequestWithHandler:^(NSData *data,
                                                                                 NSHTTPURLResponse *response,
                                                                                 NSError *error) {
                                                if(!error)
                                                {
                                                    NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
                                                                                                        options:kNilOptions
                                                                                                          error:&error];
                                                    if([list objectForKey:@"error"] != nil)
                                                    {
                                                        // if error occured e.g. Invalid Auth etc.
                                                    }
                                                    else
                                                    {
                                                        [self.arrayFriendsList removeAllObjects];

                                                        NSArray *dictFbFriend = [list objectForKey:@"data"];

                                                        for (NSDictionary *dict in dictFbFriend)
                                                        {
                                                            NSDictionary *pictureDataDict = [[dict objectForKey:@"picture"] objectForKey:@"data"];

                                                            Contacts *objContact = [[Contacts alloc] initWithName:[dict objectForKey:@"name"]
                                                                                                         andEmail:nil
                                                                                                     andFirstName:[dict objectForKey:@"first_name"]
                                                                                                      andLastName:[dict objectForKey:@"last_name"]
                                                                                                        andGender:[dict objectForKey:@"gender"] andPhotoPath:[pictureDataDict objectForKey:@"url"]
                                                                                                   andIsInstalled:[dict objectForKey:@"installed"] ? [[dict objectForKey:@"installed"] boolValue] : NO
                                                                                                    andFacebookId:[dict objectForKey:@"id"]
                                                                                    ];

                                                            [self.arrayFriendsList addObject:objContact];
                                                            objContact = nil;
                                                        }

                                                        NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
                                                        NSArray *descriptors=[NSArray arrayWithObject: descriptor];
                                                        self.arrayFriendsList = (NSMutableArray *)[self.arrayFriendsList  sortedArrayUsingDescriptors:descriptors];

                                                    }
                                                }
                                                else
                                                {
                                                    NSLog(@"Error%@", error);

                                                    //                                                    [progressHUD hide:YES];
                                                }

                                                dispatch_async(dispatch_get_main_queue(), ^{
                                                    [progressHUD hide:YES];
                                                    [self.tblFriendsList reloadData];
                                                });
                                            }];
                                        }
                                        else
                                        {
                                            NSLog(@"Grant Error:%@", error);

                                            [progressHUD hide:YES];

                                            UIAlertView *alertView = [[UIAlertView alloc]
                                                                      initWithTitle:@"Error"
                                                                      message:@"You need to set up Facebook account on the Settings App."
                                                                      delegate:self
                                                                      cancelButtonTitle:@"OK"
                                                                      otherButtonTitles:nil];
                                            [alertView show];
                                        }
                                    }];
    }
    else
    {
        [progressHUD hide:YES];

        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:@"You need to set up Facebook account on the Settings App."
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}