2014-05-12 70 views
4

我是新來的iOS應用程序開發。我想學習ios中的facebook集成和獲取好友列表。Facebook的SDK錯誤域= com.facebook.sdk代碼= 5時獲取ios中的朋友列表

我按照git的轂代碼https://github.com/facebook/facebook-ios-sdk/tree/master/samples/FriendPickerSample

並按照所有的步驟。我的代碼是:

- (IBAction)friendPicker:(id)sender { 


    if (!FBSession.activeSession.isOpen) 
    { 
     // permission : [NSArray arrayWithObjects:@"email", @"read_friendlists", @"user_photos", @"user_events", nil] 
     [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { 
      if(error) 
      { 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
       [alert show]; 

      } 
      else if (session.isOpen) 
      { 
       // [FBSession setActiveSession:session]; 
       [self friendPicker:sender]; 
      } 
     }]; 
     return; 
    } 

if(FBSession.activeSession.isOpen) 
    { 
     FBRequest* friendsRequest = [FBRequest requestForMyFriends]; 
     [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection, 
                 NSDictionary* result, 
                 NSError *error) { 
      if(error) 
      { 
       NSLog(@"error : %@",error); 
      } 
      else 
      { 

       NSArray* friends = [result objectForKey:@"data"]; 
       NSLog(@"total array : %@",result); 
       NSLog(@"Found: %i friends", friends.count); 
       for (NSDictionary<FBGraphUser>* friend in friends) { 
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id); 
       } 
      } 
     }]; 


    } 
    if(self.friendPickerController == nil) 
    { 
     self.friendPickerController = [[FBFriendPickerViewController alloc] init]; 

     self.friendPickerController.title = @"Pick Friends"; 

     self.friendPickerController.delegate = self; 
    } 

    [self.friendPickerController loadData]; 
    [self.friendPickerController clearSelection]; 

    [self presentViewController:self.friendPickerController animated:YES completion:nil]; 

} 

-(void)fillTextAndDismiss:(NSString *)text 
{ 
    [self.selectedFrdView setText:text]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void)facebookViewControllerDoneWasPressed:(id)sender 
{ 
    NSMutableString *text = [[NSMutableString alloc] init]; 

    for(id<FBGraphUser> user in self.friendPickerController.selection) 
    { 
     if([text length]) 
     { 
      [text appendString:@", "]; 
     } 

     [text appendString:user.name]; 
    } 

    [self fillTextAndDismiss:text.length >0 ? text : @"<none>"]; 

} 

-(void)facebookViewControllerCancelWasPressed:(id)sender 
{ 
    [self fillTextAndDismiss:@"<cancelled>"]; 
} 

我也創建facebookAppId和plist文件也通過這個鍵更新。 然後還我收到此錯誤:

2014-05-12 14:21:27.043 fbFrndTest[2753:90b] error : Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xb166450 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={ 
    body =  { 
     error =   { 
      code = 100; 
      message = "(#100) Unknown fields: username."; 
      type = OAuthException; 
     }; 
    }; 
    code = 400; 
}, com.facebook.sdk:ErrorSessionKey=<FBSession: 0xb521fd0, state: FBSessionStateOpen, loginHandler: 0x0, appID: 1481684055397512, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0xb523240>, expirationDate: 2014-07-11 05:48:12 +0000, refreshDate: 2014-05-12 07:26:43 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
)>} 

GitHub的代碼成功運行,但我的代碼給出了一個錯誤。

回答

0

我相信你應該取消註釋

//[FBSession setActiveSession:session]; 

,你下的設置

+0

我檢查過並且沒有評論該聲明就運行 – Max

0

擷取Facebook好友與使用FBRequest

Xcode中登錄到Facebook上你的模擬器還仔細檢查8.2.1和目標

   FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,picture,birthday,email,location"]; 
      [friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
       { 
        if(error == nil) { 

        NSArray *data = [result objectForKey:@"data"]; 
        for (FBGraphObject<FBGraphUser> *friend in data) 
        { 
        // [delegate.friendsListArray addObject:friend]; 
         NSLog(@"%@:%@:%@:", [friend name],[friend birthday],[friend id]); 
         NSString *email=[friend objectForKey:@"email"]; 
         NSLog(@"Email:%@",email); 

        } 
         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Facebook" message:@"Success in fetching Facebook Friends." delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil, nil]; 
         [alert show]; 

        } 
        else 
        { 
         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Facebook" message:@"Problem in fetching Facebook Friends. Try Later!" delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil, nil]; 
         [alert show]; 
        } 


       }];