自定义Googleloginbutton – iOS
我想要自定义Google Sign-Inbutton,如下所示:
我已经尝试下面的链接,但没有一个帮助非常: – 如何自定义谷歌loginbutton?
https://developers.google.com/identity/sign-in/ios/
请有人指导我应该做什么? 我无法使用Google+loginbutton,因为“ Google+login已弃用 ”。
编辑: – 我试过下面的链接提供的代码: –
https://developers.google.com/identity/sign-in/ios/sign-in#add_the_sign-in_button
您可以添加自己的button,而不是使用Googleloginbutton执行以下操作
目标C版本
1)将自己的button添加到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 { }
//提供一个提示用户使用Googlelogin的视图
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController { [self presentViewController:viewController animated:YES completion:nil]; }
//closures“使用Googlelogin”视图
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController { [self dismissViewControllerAnimated:YES completion:nil]; }
//完成login
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error { //user signed in //get user data in "user" (GIDGoogleUser object) }
Swift 4版本
在Swift中,确保你已经添加了briding头文件,因为这个库是用C语言编写的
1)将自己的button添加到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!) { }
//提供一个提示用户使用Googlelogin的视图
func signIn(_ signIn: GIDSignIn!, presentViewController viewController: UIViewController!) { self.present(viewController, animated: true, completion: nil) }
//closures“使用Googlelogin”视图
func signIn(_ signIn: GIDSignIn!, dismissViewController viewController: UIViewController!) { self.dismiss(animated: true, completion: nil) }
//完成login
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)") } }
编辑:这里是使用自定义button, 谷歌文档参考的参考/证据
5.如果要自定义button,请执行以下操作:在视图控制器的.h文件中,将loginbutton声明为属性。
@property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton;
将button连接到刚刚声明的signInButton属性。 通过设置GIDSignInButton对象的属性来自定义button。 接下来,您可以实现并处理注销button。
Swift 3版本
在Swift中,确保你已经添加了Briding头文件,因为这个库是用Objective C编写的。
- 把你自己的button添加到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)") } }
@Rohit KP的答案都可以(https://stackoverflow.com/a/34368678/2905967)
但分配代表时很less加。
请打电话给你这样的动作:
- (IBAction)btnGooglePlusPressed:(id)sender { [GIDSignIn sharedInstance].delegate=self; [GIDSignIn sharedInstance].uiDelegate=self; [[GIDSignIn sharedInstance] signIn]; }
并添加这些代表GIDSignInDelegate,GIDSignInUIDelegate
您可以添加自己的button,而不是使用Googleloginbutton执行以下操作
1)在AppDelegate.m文件中添加此代码
2)将自己的button添加到storyBoard中,并将类名称设置为GPPSignInButton,并在该button上设置UIImageView。
3)拖动到viewController的行动
AppDelegate.m文件
- (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); } }]; } } }
对于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)") } }