Reputation: 63
How to get username from facebook sdk 4.0 in iOS?
Upvotes: 3
Views: 7387
Reputation: 5201
*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);
}
}];}}
Upvotes: 2
Reputation: 1018
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);
}
Upvotes: 4
Reputation: 203
-(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
Upvotes: 9
Reputation: 73984
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.
Upvotes: 6