Iphone 如何在ios7中编码以获取facebook好友列表

Iphone 如何在ios7中编码以获取facebook好友列表,iphone,ios7,Iphone,Ios7,嗨,我在使用下面的代码 -(IBAction)FacebookFriendBtnClk:(id)sender { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; __block ACAccount *facebookAccount = nil; ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:

嗨,我在使用下面的代码

-(IBAction)FacebookFriendBtnClk:(id)sender
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;

ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

// Specify App ID and permissions
NSDictionary *options = @{
                          ACFacebookAppIdKey: @"743178525726332",
                          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
     }
 }];
}

我在日志中收到以下错误

  2014-04-04 16:49:04.098 ImageProcessingDemo[4791:3c0b] error.localizedDescription======= (null)
  2014-04-04 16:49:34.964 ImageProcessingDemo[4791:3f07] Dictionary contains data: {
error =     {
    code = 2500;
    message = "An active access token must be used to query information about the current user.";
    type = OAuthException;
    };
}

请尝试以下代码:-

-(void)getFrndList
{
    @try
    {
        [app showIndicator:@"Loading..."];
        if (![FBSession activeSession].isOpen) {
            [FBSession renewSystemCredentials:^(ACAccountCredentialRenewResult result,
                                                NSError *error)
             {
                 if(error)
                 {
                     [app stopIndicator];
                 }
                 NSArray *permissions = [NSArray arrayWithObjects:@"email", @"read_friendlists", @"user_photos", @"user_events", nil];
                 [FBSession openActiveSessionWithReadPermissions:permissions
                                                    allowLoginUI:YES
                                               completionHandler:
                  ^(FBSession *session,
                    FBSessionState state, NSError *error) {
                      if(error)
                      {
                          [app stopIndicator];
                      }
                      [self btnFriends:nil];
                  }];
             }];
        }
        else
        {
            [self btnFriends:nil];
        }
    }
    @catch (NSException *e) {

    }
}


-(IBAction)btnFriends:(id)sender
{

    FBRequest* friendsRequest = [FBRequest requestForMyFriends];
    [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                          NSDictionary* result,
                                                          NSError *error) {
    NSArray* friends = [result objectForKey:@"data"];
    [arrFacebookFriends removeAllObjects];
    NSLog(@"Found: %i friends", friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:[NSString stringWithFormat:@"%@ %@",friend.first_name,friend.last_name] forKey:dictkeyName];
    [dict setValue:friend.id forKey:dictkeyId];
    [dict setValue:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large",friend.id] forKey:dictkeyImage];
    [arrFacebookFriends addObject:dict];
    NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);

    }


     [app stopIndicator];
     }];

}
-(void)getFrndList
{
@试一试
{
[应用程序显示指示器:@“正在加载…”);
如果(![FBSession activeSession].isOpen){
[FBSession续订系统凭据:^(ACAccountCredential续订结果,
N错误*错误)
{
如果(错误)
{
[应用程序停止指示器];
}
NSArray*权限=[NSArray ARRAY WITHOBJECTS:@“电子邮件”,“阅读好友列表”,“用户照片”,“用户事件”,无];
[FBSession openActiveSessionWithReadPermissions:权限
阿洛洛洛吉努伊:是的
completionHandler:
^(第*届会议,
FBSessionState状态,N错误*错误){
如果(错误)
{
[应用程序停止指示器];
}
[自己的朋友:无];
}];
}];
}
其他的
{
[自己的朋友:无];
}
}
@捕获(N例外*e){
}
}
-(iAction)btnFriends:(id)发件人
{
FBRequest*friendsRequest=[FBRequestFormyFriends];
[FriendsRequestStartWithCompletionHandler:^(FBRequestConnection*连接,
NSDictionary*结果,
N错误*错误){
NSArray*friends=[result objectForKey:@“data”];
[arrFacebookFriends removeAllObjects];
NSLog(@“找到:%i个朋友”,朋友数);
用于(NSDictionary*朋友中的朋友){
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
[dict setValue:[NSString stringWithFormat:@“%@%@”,friend.first_name,friend.last_name]forKey:dictkeyName];
[dict setValue:friend.id forKey:dictkeyId];
[dict setValue:[NSString stringWithFormat:@]https://graph.facebook.com/%@/图片?类型=大“,friend.id]forKey:dictkeyImage];
[arrFacebookFriends添加对象:dict];
NSLog(@“我有一个名为%@且id为%@”、friend.name、friend.id的朋友);
}
[应用程序停止指示器];
}];
}

如何声明FBSessionhi am使用社交框架如何解决此错误您需要集成facebooksdk.framework才能使用此代码。您可以从developer.facebook.com下载它。只需下载该框架并在拖放我们要导入的文件后拖放到您的项目中您需要导入#导入您4小时前提出的问题的副本。如果要添加更多信息,请编辑原始问题,而不是创建新问题。