当前位置:  开发笔记 > IOS > 正文

自定义Google登录按钮 - iOS

如何解决《自定义Google登录按钮-iOS》经验,为你挑选了3个好方法。

我想自定义Google登录按钮,如下所示: -
在此输入图像描述
我尝试了下面的链接,但没有一个帮助真的: - 如何自定义谷歌登录按钮?
https://developers.google.com/identity/sign-in/ios/

有人可以指导我应该做什么吗?我无法使用Google+登录按钮,因为" 不赞成使用Google+登录 ".

编辑: - 我尝试了以下链接提供的代码: -
https://developers.google.com/identity/sign-in/ios/sign-in#add_the_sign-in_button



1> Rohit Pradha..:

您可以添加自己的按钮,而不是使用Google登录按钮

目标C版

1)将自己的按钮添加到storyBoard中

2)将动作拖动到viewController中

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

3)处理委托方法

#pragma mark - Google SignIn委托

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

  }

//提供一个提示用户使用Google登录的视图

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

//关闭"使用Google登录"视图

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

}

//已完成登录

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

在Swift中,确保添加了briding header,因为库是用目标C编写的

1)将自己的按钮添加到storyBoard中

2)将动作拖动到viewController中

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

3)处理委托方法

// MARK:Google SignIn代表

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

//提供一个提示用户使用Google登录的视图

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

//关闭"使用Google登录"视图

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

//已完成登录

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

编辑:以下是使用自定义按钮,Google Doc参考的参考/证据

5.如果要自定义按钮,请执行以下操作:在视图控制器的.h文件中,将登录按钮声明为属性.

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

将按钮连接到刚刚声明的signInButton属性.通过设置GIDSignInButton对象的属性来自定义按钮.接下来,您可以实现并处理注销按钮.


@TrongVu:不,没有理由拒绝App.我已经在App Store上使用自定义按钮进行了实时应用.我编辑了答案并添加了谷歌文档的参考.请参考.谢谢你的指点.

2> ShrikantWale..:

Swift 3版

在Swift中确保添加了briding header,因为库是用Objective C编写的.

    将自己的按钮添加到storyBoard中

    将动作拖动到viewController中

    @IBAction func googlePlusButtonTouchUpInside(sender: AnyObject) {
          GIDSignIn.sharedInstance().signIn()
    } 
    

    处理委托方法

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



3> 小智..:

对于Swift 4 :(这是工作代码享受)

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

推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有