How to change Default login button in FBSDKLoginBu

2020-06-18 02:49发布

问题:

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

回答1:

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");
    }
}


回答2:

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];
                                  }];
}


回答3:

FBLoginView *fbLoginView = [[FBLoginView alloc] initWithPermissions:[NSArray arrayWithObject:@"publish_actions"]];


fbLoginView.frame = CGRectMake(0, 200, 271, 37);
 for (id obj in fbLoginView.subviews)
    {
        if ([obj isKindOfClass:[UIButton class]])
        {
            UIButton * loginButton =  obj;
            UIImage *loginImage = [UIImage imageNamed:@"YourImg.png"];
            [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
            [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
            [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
            [loginButton sizeToFit];
        }
}


回答4:

I Just end up in iOS 9. put this code in button action method.

[FBSession openActiveSessionWithReadPermissions:@[@"public_profile",@"email",@"user_friends"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {


         // The session is open. Get the user information and update the UI.
         [FBRequestConnection startWithGraphPath:@"me"
                                      parameters:@{@"fields": @"first_name,last_name, email"}
                                      HTTPMethod:@"GET"
                               completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                   if (!error) {
                                       // Set the use full name.

                                       NSLog(@"FBresponse: =>%@",result);
                                   }
                                   else{
                                       NSLog(@"%@", [error localizedDescription]);
                                   }
                               }];



     }];