Custom Google Sign-In button - iOS

后端 未结 10 2503
别那么骄傲
别那么骄傲 2020-12-24 01:19

I want to customize Google Sign-In button like below:-

I have tried below links, but none of them helped really much:- How to customize google sign in button?
ht

相关标签:
10条回答
  • 2020-12-24 01:56

    In GoogleSignIn SDK 5.0 and above GIDSignInUIDelegate has been revoked

    Add this below line, for custom google login button

    @IBAction func googleLoginPressed(sender: UIButton) {
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance()?.delegate = self
        GIDSignIn.sharedInstance()?.signIn()
    }
    
    0 讨论(0)
  • 2020-12-24 01:59

    Try this for swift, its very simple and works like a champ.

    1. Create reference for your Google Sign In button

      @IBOutlet weak var signInButton: GIDSignInButton!

    2. set style for it in viewDidLoad

      override func viewDidLoad() {
      super.viewDidLoad()
      //Do any additional setup after loading the view.
        signInButton.style = GIDSignInButtonStyle.iconOnly
      

    3.Place your custom button above the google sign in button in main story board and create an action reference for it. Inside it click the google sign in button programmatically.

        @IBAction func googleSignIn(_ sender: Any) {
        signInButton.sendActions(for: .touchUpInside)
        }
    
    0 讨论(0)
  • 2020-12-24 02:02

    Swift 5.2:-

    Add below lines in your view controller
    
    func googleLoginButtonPressed(sender: UIButton) {
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance()?.delegate = self
        GIDSignIn.sharedInstance()?.signIn()
    }
    
    0 讨论(0)
  • 2020-12-24 02:06

    You can add your own button instead of using Google Sign-In button Do follwing things

    1)Add this code in AppDelegate.m file

    2)Add your own button into storyBoard and give class name as GPPSignInButton and set UIImageView on that button.

    3)drag action into viewController

    AppDelegate.m file

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        GPPSignIn *SignIn = [GPPSignIn sharedInstance];
    
        [GPPSignIn sharedInstance].clientID = @"532796865439-juut4g2toqdfc13mgqu5v9g5cliguvmg.apps.googleusercontent.com";
    
        SignIn.scopes = @[kGTLAuthScopePlusLogin];
        return YES;
    } 
    
    -(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        if ([GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation]) {
    
            return YES;
        }
    
        return wasHandled;
    }
    
    
    
    
    ViewController.m file
    
    @property (strong, nonatomic) IBOutlet GPPSignInButton *btn;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
       [GPPSignIn sharedInstance].delegate = self;
        [[GPPSignIn sharedInstance] trySilentAuthentication];
    
        AppDelegate *appDelegate = (AppDelegate *)
        [[UIApplication sharedApplication] delegate];
      }
    
    
    
    -(void) finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
    {
        GPPSignIn *signIn = [GPPSignIn sharedInstance];
        signIn.shouldFetchGoogleUserEmail = YES;
        signIn.delegate = self;
    
        if (error == nil) {
            if(auth.canAuthorize){
                GTLServicePlus *service = [[GTLServicePlus alloc] init];
                [service setRetryEnabled:YES];
                [service setAuthorizer:auth];
    
                GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];
    
    
                // 1. Create a |GTLServicePlus| instance to send a request to Google+.
                GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ;
                plusService.retryEnabled = YES;
    
                // 2. Set a valid |GTMOAuth2Authentication| object as the authorizer.
                [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];
    
                // 3. Use the "v1" version of the Google+ API.*
                plusService.apiVersion = @"v1";
                [plusService executeQuery:query
                        completionHandler:^(GTLServiceTicket *ticket,
                                            GTLPlusPerson *person,
                                            NSError *error) {
                            if (error) {
                                //Handle Error
                            } else {
                                NSLog(@"\nEmail= %@", [GPPSignIn sharedInstance].authentication.userEmail);
                                NSLog(@"\nGoogleID=%@", person.identifier);
                                NSLog(@"\nUser Name=%@", [person.name.givenName stringByAppendingFormat:@" %@", person.name.familyName]);
                                NSLog(@"\nGender=%@", person.gender);
                            }
                        }];
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-24 02:08

    All are fine with the answer of @Rohit KP (https://stackoverflow.com/a/34368678/2905967)

    But Little adding when assigning the delegates.

    Please call your action like this:

    - (IBAction)btnGooglePlusPressed:(id)sender
    {
        [GIDSignIn sharedInstance].delegate=self;
        [GIDSignIn sharedInstance].uiDelegate=self;
        [[GIDSignIn sharedInstance] signIn];
    }
    

    and add these delegates GIDSignInDelegate,GIDSignInUIDelegate

    0 讨论(0)
  • 2020-12-24 02:09

    For Swift 4.2

    to make a custom Google button :

    1-add your button to storyboard

    2-create @IBaction for your button

    3-follow instructions on https://developers.google.com/identity/sign-in/ios/sign-in but replace this step

    "2 .In the view controller, override the viewDidLoad method to set the UI delegate of the GIDSignIn object, and (optionally) to sign in silently when possible"

    with

    -> insert this code in the button action

        GIDSignIn.sharedInstance().uiDelegate=self
        GIDSignIn.sharedInstance().signIn()
    

    now you can happily customize your button,hope this answer help you.

    0 讨论(0)
提交回复
热议问题