I was following a tutorial on YouTube about a messaging app. The tutorial was old and the google SDK was updated which then made my code full of errors. For a couple of days I have been trying to fix it and even found someone else’s updated code which showed how to do it but my code still has errors. I’m new to coding and cannot figure out how to make my Google sign in button work. I have skipped over the other code and only added in the code for the google sign in button on the login View Controller.
** 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 = GIDGoogleUser 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()
}
}
}
}
}
- Look up google documentation
- Look up apple documentation
- Look up the various errors online when I made the changed and new error popped up.
“Instance member ‘profile’ cannot be used on type ‘GIDGoogleUser'”
Andrew is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1