How to get username from facebook sdk 4.0 in iOS?
Asked
Active
Viewed 7,375 times
4 Answers
9
-(IBAction)LoginWithFacebook:(id)sender {
if ([FBSDKAccessToken currentAccessToken]) {
[self getDetailsAndLogin];
}
else{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
NSLog(@"%@",error.description);
} else if (result.isCancelled) {
// Handle cancellations
NSLog(@"Result Cancelled!");
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:@"email"]) {
// Do work
[self getDetailsAndLogin];
}
}
}];
}
}
-(void)getDetailsAndLogin{
if (LOGGING) {
return;
}
LOGGING = YES;
[super startLoader];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *userID = [[FBSDKAccessToken currentAccessToken] userID];
NSString *userName = [result valueForKey:@"name"];
NSString *email = [result valueForKey:@"email"];
NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [[FBSDKAccessToken currentAccessToken] userID]];
[User LoginWithFbId:userID Username:userName Email:email ImageUrl:userImageURL success:^(User *response) {
[super stopLoader];
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
TabViewController *TabVC = [sb instantiateViewControllerWithIdentifier:@"TabViewController"];
[self.navigationController pushViewController:TabVC animated:YES];
} failure:^(NSString *error) {
LOGGING = NO;
[super stopLoader];
[super showAlertWithTitle:@"Cannot Login" Message:error];
}];
}
else{
LOGGING = NO;
[super stopLoader];
NSLog(@"%@",error.localizedDescription);
}
}];
}
here LoginWithFacebook
is a button action to get data . Do not forget to import SDK of FBSession which you can get easily from here . Register your app create a key and import this key in your application.
Happy coding

Sushil Sharma
- 2,321
- 3
- 29
- 49

Arun Yadav
- 203
- 3
- 9
6
You can´t get the username anymore:
/me/username is no longer available.
Source: https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api
If you want to detect returning users, use the (App Scoped) ID instead.

andyrandy
- 72,880
- 8
- 113
- 130
4
Easiest Answer would be to check the following after user is logged in:
if ([FBSDKProfile currentProfile])
{
NSLog(@"User name: %@",[FBSDKProfile currentProfile].name);
NSLog(@"User ID: %@",[FBSDKProfile currentProfile].userID);
}

Mohsin Khubaib Ahmed
- 1,008
- 16
- 32
-
2This may not necessarily be safe (as I have learnt the hard way). currentProfile has sometimes not finished being fetched even if currentAccessToken is not null. in this case you may get a null profile. – JCutting8 Feb 04 '16 at 05:15
-
if ([FBSDKProfile currentProfile]) { NSLog(@"User name: %@",[FBSDKProfile currentProfile].name); NSLog(@"User ID: %@",[FBSDKProfile currentProfile].userID); } add this checking to avoid null – user1969245 Jul 07 '16 at 00:18
2
*Use my code its works excellent.
- (IBAction)tapon_facebookLogin:(id)sender {
if ([FBSDKAccessToken currentAccessToken]) {
// TODO:Token is already available.
NSLog(@"FBSDKAccessToken alreay exist");
[self fetchFbUserInfo];
}else{
NSLog(@"FBSDKAccessToken not exist");
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:@[@"email",@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
//TODO: process error or result
if (!error) {
NSLog(@"result %@",result.debugDescription);
[self fetchFbUserInfo];
}else{
NSLog(@"errorfacebook %@",error.description);
}
}];
}}
-(void)fetchFbUserInfo{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"resultisfetchFbUserInfo:%@",result);
}
else
{
NSLog(@"ErrorfetchFbUserInfo %@",error);
}
}];}}

Mohit Tomar
- 5,173
- 2
- 33
- 40