2015-02-09 223 views
0

一直使用FBConnect來選擇朋友,但FBSession的accessTokenData從來沒有啓動會話的用戶的用戶標識(在他們的登錄演示中也不存在)。 FBFriendPickerViewController返回朋友用戶的ID,但我沒有看到關於當前用戶的任何信息。是否有可能從FB活動會話,accessTokenData或其他方式獲取當前用戶的ID?FBSession.session.accessTokenData.userID始終爲零

編輯: 是否有可能從此調用返回用戶的ID?

[FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"] 

這樣做一步到位會好得多。

回答

0

您必須申請「/我」才能獲得'user_id',因爲access_token屬性是加密的,您無法通過這種方式訪問​​它。

+0

謝謝,意識到我需要完成鏈處理。不確定它非常直觀。 – user1192805 2015-02-09 15:18:44

0

這裏是我落得這樣做:

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) { 

            if ([session.declinedPermissions containsObject:@"user_friends"]) { 
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"friends" 
                         message:@"Must allow accessing friends" 
                         delegate:nil 
                       cancelButtonTitle:@"OK" 
                       otherButtonTitles:nil]; 
            [alertView show]; 
            } 
            else { 
            [[FBRequest requestForMe] startWithCompletionHandler: 
            ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 

             if (error) { 
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                          message:error.localizedDescription 
                          delegate:nil 
                        cancelButtonTitle:@"OK" 
                        otherButtonTitles:nil]; 
             [alertView show]; 
             } 
             else { 
             NSLog(@"name %@ id %@", user.first_name, user.objectID); 

             [[FBRequest requestForMyFriends] 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.objectID); 
              } 
             }]; 
             } 
            }]; 
            } 
           } 
           }];