Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Default login button in FBSDKLoginButton in ios?

How to Customize the Login button in Facebook? I don't want to use Facebook default login button.

like image 825
Deepak Bradly Avatar asked Apr 21 '15 08:04

Deepak Bradly


2 Answers

For SDK 4.0 :

U should add a button and in button action use the below code :

- (IBAction)loginButtonClicked:(id)sender {

   FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
   [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {
            // Process error
            NSLog(@"error %@",error);                
        } else if (result.isCancelled) {
            // Handle cancellations
           NSLog(@"Cancelled");
        } else {
           if ([result.grantedPermissions containsObject:@"email"]) {
                // Do work
                NSLog(@"%@",result);
                NSLog(@"Correct");
            }
        }
    }];
}

Updated : To receive Users Information

 - (IBAction)loginButtonClicked:(id)sender {

       FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
       [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
            if (error) {
                // Process error
                NSLog(@"error %@",error);                
            } else if (result.isCancelled) {
                // Handle cancellations
               NSLog(@"Cancelled");
            } else {
               if ([result.grantedPermissions containsObject:@"email"]) {
                    // Do work
                     [self fetchUserInfo];
                }
            }
        }];
    }

-(void)fetchUserInfo {

    if ([FBSDKAccessToken currentAccessToken]) {

    NSLog(@"Token is available");

    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSLog(@"Fetched User Information:%@", result);

         }
         else {
             NSLog(@"Error %@",error);
         }
     }];

    } else {

        NSLog(@"User is not Logged in");
    }
}
like image 105
Dheeraj Singh Avatar answered Oct 06 '22 00:10

Dheeraj Singh


You should add a UIButton and add a click method like this:

- (IBAction)fbLogin:(id)sender {
    [FBSession.activeSession closeAndClearTokenInformation];
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *fbSession, FBSessionState state, NSError *error) {
                                      [self sessionStateChanged:fbSession state:state error:error];
                                  }];
}
like image 20
Zec Zhang Avatar answered Oct 06 '22 00:10

Zec Zhang