2014-05-08 34 views
0

我試圖讓Facebook好友列表,並在自定義視圖,如「foresqure應用程序」顯示它獲取Facebook好友列表並將其顯示在自定義視圖中?

我是什麼意思「四方」要求的許可,並在應用程式內牆上張貼無需離開應用程序到瀏覽器或Facebook的應用程序?之後,該應用程序獲取所有朋友並將其顯示在自定義tableView中。

enter image description here

這裏是我的嘗試:

if (!FBSession.activeSession.isOpen) { 
    // if the session is closed, then we open it here, and establish a handler for state changes 
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"] 
             allowLoginUI:YES 
            completionHandler:^(FBSession *session, 
                 FBSessionState state, 
                 NSError *error) { 
             if (error) { 
              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                           message:error.localizedDescription 
                          delegate:nil 
                        cancelButtonTitle:@"OK" 
                        otherButtonTitles:nil]; 
              [alertView show]; 
             } else if (session.isOpen) { 
              //[self pickFriendsButtonClick:sender]; 
              NSLog(@"aaaa"); 
              FBRequest* friendsRequest = [FBRequest requestForMyFriends]; 
              [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection, 
                         NSDictionary* result, 
                         NSError *error) { 
               NSArray* friends = [result objectForKey:@"data"]; 
               NSLog(@"Found: %i friends", friends.count); 
               for (NSDictionary<FBGraphUser>* friend in friends) { 
                NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id); 
               } 
              }]; 
             } 
            }]; 

} 

在這種情況下,它會打開Safari索要permision不是應用程序和朋友陣列內允許nill或數= 0

+0

檢查我的答案。 –

+0

@DarshanKunjadiya在我的mac中檢查這個http://www.filedropper.com/testface – Omarj

+0

這段代碼是完美的。我只更改我的Facebook應用程序ID並獲取所有朋友列表。 –

回答

0

試試這個代碼..

首先這樣調用這個方法..

[self FBFriendList]; 

並添加這兩個方法。

-(void)FBFriendList 
{ 
if (!FBSession.activeSession.isOpen) 
{ 
    // if the session is closed, then we open it here, and establish a handler for state changes 

    [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error) 
    { 
     if (error) 
     { 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alertView show]; 
      [alertView release]; 
     } 
     else if(session.isOpen) 
     { 
      [self FBFriendList]; 
      [self fb_frnd_use_this_app]; 
     } 
    }]; 

    return; 
} 
} 

-(void)fb_frnd_use_this_app 
    { 
    FBRequest *request = [FBRequest requestWithGraphPath:@"me/friends" 
               parameters:@{@"fields":@"name,installed,first_name,picture"} 
               HTTPMethod:@"GET"]; 

    [request startWithCompletionHandler:^(FBRequestConnection *connection, 
              id result, 
              NSError *error) 
    { 


     NSLog(@"%@",result); 
     int count = 0; 
     NSMutableArray *frnd_arr = [result objectForKey:@"data"]; 
     for (int i = 0; i < [frnd_arr count]; i++) 
     { 
      NSLog(@"%@",frnd_arr); 
      count++; 
     } 

     NSLog(@"%@",app.Arr_Facebook_Frnd); 
     app.fbfriendCount =[NSString stringWithFormat:@"%d",count]; 

    }]; 
} 

希望這段代碼對您有用。

+0

同樣的結果打開:打開Safari瀏覽器以獲得不是我想要的權限:( – Omarj

+0

@Omarj權限是獲取Facebook好友列表的必要條件。 –

+0

我知道,我正在嘗試的是Git像Foursqure應用程序一樣的權限看看我附加的照片 – Omarj

相關問題