2012-09-15 20 views
2

我正在開發iOS 5應用程序,我需要在成功登錄後獲取用戶配置文件數據。在iOS 5中獲取訪問令牌後獲取Facebook用戶配置文件數據

現在我得到一個訪問令牌,當用戶成功登錄。

現在我試圖讓Facebook使用該訪問令牌登錄用戶配置文件數據。

我試圖按照Facebook開發者網站上提供的示例應用程序。

我沒有使用FBconnect類型的資源文件。我從示例應用程序添加「facebookSDK.framework」。

請幫幫我。

感謝,

- (void)populateUserDetails { 

FBRequestConnection *connection = [[FBRequestConnection alloc] init]; 

// First request uploads the photo. 
FBRequest *request1 = [FBRequest requestForMe]; 


if (FBSession.activeSession.isOpen) { 

    [connection addRequest:request1 
     completionHandler: 
    ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 
     if (!error) { 

      NSLog(@"fb user name = %@",user.name); 
      NSLog(@"fb user name = %@",user.id); 
     } 

     NSLog(@"fb2 user name = %@",user.name); 
     NSLog(@"fb2 user name = %@",user.id); 
    } 
      //batchEntryName:@"photopost" 
    ]; 

    [connection start]; 
} 

}

+0

如果條件與if(objappdel.session.isOpen)中的上述代碼一樣,則代碼將執行,但會給出錯誤代碼400.並記錄空值。 會話屬性類型:@property(強,非原子)FBSession *會話; –

回答

12

在您的.h

#import<FacebookSDK/FacebookSDK.h> 

現在,在您.m文件只需要調用下面的方法來獲取用戶數據

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{ 
    NSLog(@"usr_id::%@",user.id); 
    NSLog(@"usr_first_name::%@",user.first_name); 
    NSLog(@"usr_middle_name::%@",user.middle_name); 
    NSLog(@"usr_last_nmae::%@",user.last_name); 
    NSLog(@"usr_Username::%@",user.username); 
    NSLog(@"usr_b_day::%@",user.birthday); 

    } 

以上方法在FBLo中定義ginView.h你可以看到那裏。

我已經添加了這個方法根據最新的facebookSDK.framework.Hope它會幫助你。

+0

感謝Murali ..任何想法我怎麼能得到用戶的profile_image url –

+1

你可以通過傳遞用戶ID到FBProfilePictureView class.for我們必須採取一個插座作爲IBOutlet FBProfilePictureView * profilePic到UIImageView.Now上面的方法,loginViewFetchedUserInfo:,傳遞用戶ID像self.profilePic.profileID = user.id;現在你可以看到你的個人資料照片。 – iSpark

4

這是爲了獲取用戶的個人資料圖片的方法:

//in your app delegate file configure "FBProfilePictureView" class 
- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    [FBProfilePictureView class]; 
} 

//in your header file 
IBOutlet FBProfilePictureView *userPictureView; 


@property (strong, nonatomic) FBProfilePictureView  *userPictureView; 

//here "userPictureView" is uiview not uiimageview 

//in your .m file 

-(void)setProfilePicture { 
    [FBSession.activeSession closeAndClearTokenInformation]; 

    NSArray *permissions = [NSArray arrayWithObjects:@"email", nil]; 

    [FBSession openActiveSessionWithReadPermissions:permissions 
             allowLoginUI:YES 
            completionHandler: 
    ^(FBSession *session, 
     FBSessionState state, NSError *error) { 
     NSLog(@"\nfb sdk error = %@", error); 
     switch (state) { 
      case FBSessionStateOpen: 
       [[FBRequest requestForMe] startWithCompletionHandler: 
        ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 
         if (!error) { 

          self.userPictureView.profileID = [user objectForKey:@"id"]; 

         } 
        }]; 
       break; 
      case FBSessionStateClosed: 
       //need to handle 
       break; 
      case FBSessionStateClosedLoginFailed: 
       //need to handle 
       break; 
      default: 
       break; 
     } 
    }]; 
} 
1

我覺得效果最好,如果你寫的完成處理程序來處理結果作爲FBGraphObject:當我更換

[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, FBGraphObject *result, NSError *error) { 
    if (result && !error) { 
    name = result[@"name"]; 
    email = result[@"email"]; 
    facebookId = result[@"id"]; 
    } else { 
    NSLog(@"Error!"); 
    } 
}];