【问题标题】:Swift - Google Authentication斯威夫特 - 谷歌身份验证
【发布时间】:2017-06-28 05:31:43
【问题描述】:

我正在使用 Swift 3 并尝试使用此 tutorial 进行 Google 登录

创建了一个桥接头文件:

#import <Google/SignIn.h>

我的构建设置引用了正确的头文件

然而,当我去实现 App Delegate 时,我收到了这个错误:

*Use of undeclared type GIDSignInDelegate*

我可能会错过什么?

【问题讨论】:

  • 再次检查包含 GIDSignInDelegate 的文件的目标成员资格。你把它添加到你的主要目标了吗?

标签: ios swift3 google-signin


【解决方案1】:

请不要导入任何像谷歌这样的框架。如果您在头桥文件中正确添加,Xcode 会自动访问所需的模块。

并使用以下方法消除您的错误。

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

【讨论】:

    【解决方案2】:

    要解决您的问题,您需要执行以下操作:

    1. 导入你提到的教程中提到的pod
    2. pod 安装
    3. 在 appDelegate 中。快速导入以下import GoogleSignIn
    4. 实现以下方法:

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

    还有这个功能:

     func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
            return GIDSignIn.sharedInstance().handle(url,sourceApplication: sourceApplication, annotation: annotation)
        }
    
    1. 确保在您的视图控制器中包含以下导入

      import FirebaseAuth
      import GoogleSignIn
      
    2. 确保您的视图控制器实现以下内容

      GIDSignInUIDelegate , GIDSignInDelegate

    3. 在 viewDidLoad 函数中添加以下内容:

      GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
      GIDSignIn.sharedInstance().delegate = self
      GIDSignIn.sharedInstance().uiDelegate = self
      
    4. 最后实现SignIn方法

      func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
      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)")
      }
      

      }

    5. 在您的故事板中添加一个视图并使其从 GIDSignInButton 扩展

      class= GIDSignInButton

    6. 如果你想实现注销方法:只需将以下方法添加到同一个视图控制器

      func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!, withError error: NSError!) {
      // Perform any operations when the user disconnects from app here.
      // ...
      }
      
    7. 运行项目,当您在添加了上述方法的视图控制器中时,您将获得 google 登录按钮,单击它会打开一个 webView,最后使用您的 google 登录帐户,您将被重定向回应用程序

    PS:我不知道为什么 google 将登录和注销方法都称为 SIGNIN ,他们需要在库中进行更改。要获得更好的解释,您需要更好地阅读您链接的教程。 如果您有任何问题,我期待着阅读您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多