I am working on an iOS 5 app where i need to get user profile data after successful login.
For now I am getting a access token when user get successful login.
<This is the method to get user profile picture:
//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;
}
}];
}
in your .h
#import<FacebookSDK/FacebookSDK.h>
Now in Your .m file just call below method to get user data
-(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);
}
Above method is defined in FBLoginView.h you can see there.
This method i have added According to latest facebookSDK.framework.Hope it will help you.
I find it works best if you write the completion handler to treat the result as a 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!");
}
}];