实例成员“个人资料”不能在“GIDGoogleUser”类型上使用

问题描述 投票:0回答:1

我正在观看 YouTube 上有关消息应用程序的教程。该教程很旧,并且谷歌 SDK 已更新,这使得我的代码充满了错误。几天来我一直在尝试修复它,甚至发现其他人的更新代码显示了如何做到这一点,但我的代码仍然有错误。我是编码新手,不知道如何让我的 Google 登录按钮正常工作。我跳过了其他代码,只添加了登录视图控制器上的谷歌登录按钮的代码。

class LoginViewController: UIViewController {

// More code for login options

   private let googleLoginButton: GIDSignInButton = {
        let button = GIDSignInButton()
        button.style = GIDSignInButtonStyle.wide
        button.colorScheme = GIDSignInButtonColorScheme.dark
        return button

 override func viewDidLoad() {
        super.viewDidLoad()

 googleLoginButton.addTarget(self,
                                    action: #selector(googleButtonTapped),
                                    for: .touchUpInside)

        scrollView.addSubview(googleLoginButton)

googleLoginButton.frame = CGRect(x: 30,
                                         y: facebookLoginButton.bottom+10,
                                         width: scrollView.width-60,
                                         height: 52)


// line 249 down 
    @objc func googleButtonTapped() {
        guard let clientID = FirebaseApp.app()?.options.clientID else { return }
            let config = GIDConfiguration(clientID: clientID)
            GIDSignIn.sharedInstance.configuration = config
        GIDSignIn.sharedInstance.signIn(withPresenting: self) { result, error in
            
            guard error == nil else {
                print("Fail to sign in with Google: \(error)")
                return
                
            }
            
            guard let user = result?.user else {
                return
            }
            
            guard let email = user.profile?.email,
                  let firstName = user?.profile?.givenName,
                  let lastName = user?.profile?.familyName else {
                return
            }
            
            UserDefaults.standard.set(email, forKey: "email")
            UserDefaults.standard.set("\(firstName) \(lastName)", forKey: "name")
            
           
            
            print("Did sign in with Google: \(user)")
            DatabaseManager.shared.userExists(with: email) { exists in
                if !exists {
                    // insert to database
                    let chatUser = ChatAppUser(firstName: firstName,
                                               lastName: lastName,
                                               emailAddress: email)
                    DatabaseManager.shared.insertUser(with: chatUser) { success in
                        if success {
                            // upload image
                            guard let userProfile = user.profile else {
                                return
                            }
                            if userProfile.hasImage {
                                guard let url = userProfile.imageURL(withDimension: 200) else {
                                    return
                                }
                                
                                URLSession.shared.dataTask(with: url) { data, _, error in
                                    guard let data = data else {
                                        return
                                    }
                                    
                                    let fileName = chatUser.profilePictureFileName
                                    StorageManager.shared.uploadProfilePicture(with: data,
                                                                               fileName: fileName) { result in
                                        switch result {
                                        case .success(let downloadUrl):
                                            UserDefaults.standard.set(downloadUrl, forKey: "profile_picture_url")
                                            print(downloadUrl)
                                        case .failure(let error):
                                            print("Storage manager error: \(error)")
                                        }
                                    }
                                }.resume()
                            }
                        }
                    }
                }
            }
 let authentication = user.authentication
            guard let idToken = authentication.idToken else { return }
            let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                           accessToken: authentication.accessToken)
            
            FirebaseAuth.Auth.auth().signIn(with: credential) { [weak self]authResult, error in
                guard let strongSelf = self else {
                    return
                }
                guard authResult != nil, error == nil else {
                    print("failed to log in google credential")
                    return
                }
                
                print("Successfully signed in google credential.")
                NotificationCenter.default.post(name: .didLogInNotification, object: nil)
                strongSelf.navigationController?.dismiss(animated: true, completion: nil)
                
            }
        }
    }
  • 查找谷歌文档
  • 查找苹果文档
  • 修改后网上查各种错误,又弹出新的错误。

“GIDGoogleUser”类型的值没有成员“身份验证”

swift
1个回答
0
投票

我相信这里有一个误解。根据 Google Signin 文档,

GoogleAuthProvider
需要
idToken
accessToken.tokenString
。应该是:

GIDSignIn.sharedInstance.signIn(withPresenting: self) { result, error in
    ...
    guard let user = result?.user else {
        return
    }

    guard let user = result?.user,
          let idToken = user.idToken?.tokenString else {
        return    
    }
    ...
    let credential = GoogleAuthProvider.credential(withIDToken: idToken,
                                                   accessToken: user.accessToken.tokenString)
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.