Custom Google Sign-In button - iOS

后端 未结 10 2504
别那么骄傲
别那么骄傲 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 02:10

    Swift-5 Copy paste and Enjoy

    @IBAction func btngoogle(_ sender: UIButton) {
        GIDSignIn.sharedInstance().signIn()
    }
    
    
    //MARK:Google SignIn Delegate
    func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
        // myActivityIndicator.stopAnimating()
    }
    // Present a view that prompts the user to sign in with Google
    func sign(_ signIn: GIDSignIn!,
              present viewController: UIViewController!) {
        self.present(viewController, animated: true, completion: nil)
    }
    
    // Dismiss the "Sign in with Google" view
    func sign(_ signIn: GIDSignIn!,
              dismiss viewController: UIViewController!) {
        self.dismiss(animated: true, completion: nil)
    }
    
    //completed sign In
    public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    
        if (error == nil) {
            // Perform any operations on signed in user here.
            let userId = user.userID                  // For client-side use only!
            let idToken = user.authentication.idToken // Safe to send to the server
            let fullName = user.profile.name
            let givenName = user.profile.givenName
            let familyName = user.profile.familyName
            let email = user.profile.email
            // ...
        } else {
            print("\(error.localizedDescription)")
        }
    }
    
    0 讨论(0)
  • 2020-12-24 02:15

    For Swift 4: (This is working code Enjoy)

    @IBAction func logimByGoogle(_ sender: Any) {
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().signIn()
    }
    //MARK:- Google Delegate
    func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
    
    }
    
    func sign(_ signIn: GIDSignIn!,
              present viewController: UIViewController!) {
        self.present(viewController, animated: true, completion: nil)
    }
    
    public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
                       withError error: Error!) {
        if (error == nil) {
            // Perform any operations on signed in user here.
            let userId = user.userID                  // For client-side use only!
            let idToken = user.authentication.idToken // Safe to send to the server
            let fullName = user.profile.name
            let givenName = user.profile.givenName
            let familyName = user.profile.familyName
            let email = user.profile.email
            // ...
        } else {
            print("\(error)")
        }
    }
    
    0 讨论(0)
  • 2020-12-24 02:20

    Swift 3 Version

    In Swift make sure you have added briding header as the library is written in objective C.

    1. Add your own button into storyBoard
    2. drag action into viewController

      @IBAction func googlePlusButtonTouchUpInside(sender: AnyObject) {
            GIDSignIn.sharedInstance().signIn()
      } 
      
    3. handle delegate methods

      //MARK:Google SignIn Delegate
       func signInWillDispatch(signIn: GIDSignIn!, error: NSError!) {
        // myActivityIndicator.stopAnimating()
          }
      
      // Present a view that prompts the user to sign in with Google
         func sign(_ signIn: GIDSignIn!,
                    present viewController: UIViewController!) {
              self.present(viewController, animated: true, completion: nil)
          }
      
      // Dismiss the "Sign in with Google" view
       func sign(_ signIn: GIDSignIn!,
                    dismiss viewController: UIViewController!) {
              self.dismiss(animated: true, completion: nil)
          }
      
      //completed sign In    
      public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
      
              if (error == nil) {
            // Perform any operations on signed in user here.
                  let userId = user.userID                  // For client-side use only!
                 let idToken = user.authentication.idToken // Safe to send to the server
                  let fullName = user.profile.name
                 let givenName = user.profile.givenName
                 let familyName = user.profile.familyName
                 let email = user.profile.email
                // ...
              } else {
                  print("\(error.localizedDescription)")
              }
          }
      
    0 讨论(0)
  • 2020-12-24 02:21

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

    Objective C Version

    1)Add your own button into storyBoard

    2)drag action into viewController

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

    3)handle delegate methods

    #pragma mark - Google SignIn Delegate

    - (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
    
      }
    

    // Present a view that prompts the user to sign in with Google

    - (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController
    {
        [self presentViewController:viewController animated:YES completion:nil];
    }
    

    // Dismiss the "Sign in with Google" view

    - (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
        [self dismissViewControllerAnimated:YES completion:nil];
    
    }
    

    //completed sign In

    - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
         withError:(NSError *)error {
    //user signed in
    //get user data in "user" (GIDGoogleUser object)
    }
    

    Swift 4 Version

    In Swift make sure you have added briding header as the library is written in objective C

    1)Add your own button into storyBoard

    2)drag action into viewController

    @IBAction func googlePlusButtonTouchUpInside(sender: AnyObject) {
          GIDSignIn.sharedInstance().delegate=self
          GIDSignIn.sharedInstance().uiDelegate=self
          GIDSignIn.sharedInstance().signIn()
    } 
    

    3)handle delegate methods

    //MARK:Google SignIn Delegate

    func signInWillDispatch(_ signIn: GIDSignIn!, error: Error!) {
    }
    

    // Present a view that prompts the user to sign in with Google

    func signIn(_ signIn: GIDSignIn!,
        presentViewController viewController: UIViewController!) {
      self.present(viewController, animated: true, completion: nil)
    }
    

    // Dismiss the "Sign in with Google" view

    func signIn(_ signIn: GIDSignIn!,
        dismissViewController viewController: UIViewController!) {
      self.dismiss(animated: true, completion: nil)
    }
    

    //completed sign In

       public func signIn(_ signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
          withError error: Error!) {
            if (error == nil) {
              // Perform any operations on signed in user here.
              let userId = user.userID                  // For client-side use only!
              let idToken = user.authentication.idToken // Safe to send to the server
              let fullName = user.profile.name
              let givenName = user.profile.givenName
              let familyName = user.profile.familyName
              let email = user.profile.email
              // ...
            } else {
              print("\(error.localized)")
            }
        }
    

    Edit: Here is the reference/evidence for usage of custom button, Google Doc reference

    In these examples, the view controller is a subclass of UIViewController. If, in your project, the class that implements GIDSignInUIDelegate is not a subclass of UIViewController, implement the signInWillDispatch:error:, signIn:presentViewController:, and signIn:dismissViewController: methods of the GIDSignInUIDelegate protocol. Also don't forget to set UI delegate GIDSignIn.sharedInstance()?.uiDelegate = self

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