更改 GoogleSignIn pod 版本时使用未声明的类型“GIDSignInUIDelegate”

Posted

技术标签:

【中文标题】更改 GoogleSignIn pod 版本时使用未声明的类型“GIDSignInUIDelegate”【英文标题】:Use of undeclared type 'GIDSignInUIDelegate' when changing GoogleSignIn pod version 【发布时间】:2020-01-20 06:29:10 【问题描述】:

将 pod 版本的 GoogleSignIn 从以前的版本更改为 v5.0.0 时出现问题。

【问题讨论】:

欢迎入栈,请看***.com/help/how-to-answer。 【参考方案1】:

您必须将 GIDSignInUIDelegate 更改为 GIDSignInDelegate,如 GoogleSignIn 文档中的示例:

https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift

【讨论】:

【参考方案2】:

GIDSignInUIDelegate 协议过去必须在启动GIDSignIn.sharedInstance().signIn() 的 UIViewController 中实现。该 UIViewController 必须使用 GIDSignIn.sharedInstance().uiDelegate = self 注册自己。

自 GoogleSignIn 5.0.0 以来,这种情况发生了变化。启动 GIDSignIn.sharedInstance().signIn() 的 UIViewController 应该使用 GIDSignIn.sharedInstance()?.presentingViewController = self 注册自己,并且显然不再需要实现不再存在的 GIDSignInUIDelegate

GIDSignInDelegate 仍然打算在 AppDelegate 中实现,它应该使用 GIDSignIn.sharedInstance().delegate = self 注册自己。

另请参阅:https://developers.google.com/identity/sign-in/ios/quick-migration-guide

【讨论】:

【参考方案3】:

无需使用GIDSignInUIDelegate,查看此链接获取迁移指南:https://developers.google.com/identity/sign-in/ios/quick-migration-guide#migrating_from_versions_prior_to_v500

只需使用GIDSignInDelegate 并将GIDSignIn.sharedInstance().uiDelegate = self 替换为GIDSignIn.sharedInstance()?.presentingViewController = self

GIDSignIn.sharedInstance().handle(url,
    sourceApplication: sourceApplication,
    annotation: annotation)

GIDSignIn.sharedInstance().handle(url)

这是我的应用因UIWebView而被拒绝的地方

【讨论】:

【参考方案4】:

不要使用 GIDSignIn.sharedInstance().uiDelegate = self 和 GIDSignInUIDelegate 协议。仅使用 GIDSignInDelegate 协议并导入 GoogleUtilities 。实现 sigin 方法。

import UIKit
import Firebase
import GoogleSignIn
import FirebaseAuth
import GoogleUtilities

class LoginViewController: UIViewController, GIDSignInDelegate  
    

    override func viewDidLoad() 
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance()?.presentingViewController = self
        
    
    

    
    @IBAction func googleSignIn(sender: AnyObject) 
        GIDSignIn.sharedInstance().signIn()
    
    
    
    
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) 
        print("Google Sing In didSignInForUser")
        if let error = error 
            print(error.localizedDescription)
            return
        
        guard let authentication = user.authentication else  return 
        let credential = GoogleAuthProvider.credential(withIDToken: (authentication.idToken)!, accessToken: (authentication.accessToken)!)// When user is signed in
        Auth.auth().signIn(with: credential, completion:  (user, error) in
            if let error = error 
                print("Login error: \(error.localizedDescription)")
                return
                )  
    

【讨论】:

【参考方案5】:

如果您使用的是最新的 google 唱歌 sdk,请在按钮操作上使用此代码

 let signInConfig = GIDConfiguration.init(clientID: KGoogle.clientID)

GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self)  user, error in
    guard error == nil else  return 
    guard let user = user else  return 

    if let profiledata = user.profile 
        
        let userId : String = user.userID ?? ""
        let givenName : String = profiledata.givenName ?? ""
        let familyName : String = profiledata.familyName ?? ""
        let email : String = profiledata.email
        
        if let imgurl = user.profile?.imageURL(withDimension: 100) 
            let absoluteurl : String = imgurl.absoluteString
            //HERE CALL YOUR SERVER API
        
    
    

【讨论】:

以上是关于更改 GoogleSignIn pod 版本时使用未声明的类型“GIDSignInUIDelegate”的主要内容,如果未能解决你的问题,请参考以下文章

Cocoapods:确定 pod 版本

烦人的 Firebase 登录 Xcode 控制台

当我使用 googleSignIn 时,用户在 Firestore 上重复

Google 登录 CocoaPods 已弃用

Cocoapods pod 创建目标并在使用时更改 Xcode 中的目标

Android GoogleSignIn:多个帐户同时+失败时延迟登录