简体   繁体   English

什么是 Google 登录的正确 iOS 代码

[英]What is the correct iOS code for Google Sign-in

I recently updated my GoogleSignin pod from 3.x to 4.1.1 and the docs linked from here:我最近将我的 GoogleSignin pod 从 3.x 更新到 4.1.1 以及从这里链接的文档:

https://developers.google.com/identity/sign-in/ios/sdk/ https://developers.google.com/identity/sign-in/ios/sdk/

which go to here:去这里:

https://developers.google.com/identity/sign-in/ios/sign-in https://developers.google.com/identity/sign-in/ios/sign-in

are out of date.已经过时了。

  1. They say the import is:他们说进口是:

#import <Google/SignIn.h>

  1. They have some setup code:他们有一些设置代码:

     NSError* configureError; [[GGLContext sharedInstance] configureWithError: &configureError]; NSAssert(!configureError, @"Error configuring Google services: %@", configureError);

That seems to be out of date.这似乎已经过时了。

I could not find 4.1.x specific docs.我找不到 4.1.x 特定的文档。

(I mean this question mostly as a way to get Google Identity to update the docs. I know the answer, which I will post below) (我的意思是这个问题主要是为了让 Google Identity 更新文档。我知道答案,我将在下面发布)

Bellow code for Pod file Pod 文件的波纹管代码

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.2'
use_frameworks!

target ’App Target’ do
pod 'GoogleSignIn'
end

Then into AppDelegate.swift file add Header import GoogleSignIn Then然后在 AppDelegate.swift 文件中添加 Header import GoogleSignIn然后
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { GIDSignIn.sharedInstance().clientID = "googlesigninclient id"; }

func application(_ app: UIApplication, open url: URL, options: 
[UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return GIDSignIn.sharedInstance().handle(url,
                                             sourceApplication: 
 options[UIApplicationOpenURLOptionsKey.sourceApplication] as? 
String,
                                                annotation: 
 options[UIApplicationOpenURLOptionsKey.annotation])
}

Now in ViewController class现在在 ViewController 类中

class LoginViewController:UIViewController 
,GIDSignInUIDelegate, GIDSignInDelegate { 

  //Add Button action for gmail login 
 @IBAction func loginWithGmail(_ sender: Any) {

    GIDSignIn.sharedInstance().signIn()


}


 //deleage method will call after gmail login 
 func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, 
 withError error: Error!) {
     if (error == nil) {//login success
     } else {
      //login fail
     }

 }
}

Luckily, the github example repo is maintained.幸运的是,github 示例存储库得到维护。 According to this commit:根据这个承诺:

https://github.com/googlesamples/google-services/commit/39c243954be0e730963f79720265839f1c0b6deb https://github.com/googlesamples/google-services/commit/39c243954be0e730963f79720265839f1c0b6deb

The correct import is:正确的导入是:

#import <GoogleSignIn/GoogleSignIn.h>

(or use @import GoogleSignIn if you want) (如果需要,也可以使用@import GoogleSignIn

GGLContext seems to have been removed, so the code that uses it should be removed. GGLContext似乎已被删除,因此应该删除使用它的代码。 There doesn't seem to be some other equivalent that needs to be put in its place.似乎没有其他等价物需要放置在它的位置。

With these two changes, Google signin worked for me.通过这两项更改,Google 登录对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM