Reputation: 7
I am trying to implement the google sign in button for my app in swift and I copied the code from the google dev website but whenever I run the app and click the google sign in button i get the "thread 1: exception: presentingviewcontroller must be set." error. My view controller is correct as it is the same to the google dev site code. Is something wrong with anything else? Error image
import UIKit
import GoogleSignIn
//import FBSDKLoginKit
@objc(ViewController)
// [START viewcontroller_interfaces]
class ViewController: UIViewController {
// [START viewcontroller_vars]
@IBOutlet weak var signInButton: GIDSignInButton!
@IBOutlet weak var signOutButton: UIButton!
@IBOutlet weak var disconnectButton: UIButton!
@IBOutlet weak var statusText: UILabel!
// [END viewcontroller_vars]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
GIDSignIn.sharedInstance()?.presentingViewController = self
// Automatically sign in the user.
GIDSignIn.sharedInstance()?.restorePreviousSignIn()
// [START_EXCLUDE]
NotificationCenter.default.addObserver(self,
selector: #selector(ViewController.receiveToggleAuthUINotification(_:)),
name: NSNotification.Name(rawValue: "ToggleAuthUINotification"),
object: nil)
statusText.text = "Initialized Swift app..."
toggleAuthUI()
// [END_EXCLUDE]
}
// [END viewdidload]
Upvotes: 1
Views: 2355
Reputation: 71
Write one line of code GIDSignIn.sharedInstance()?.presentingViewController = self in func viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance()?.presentingViewController = self
}
Upvotes: 3
Reputation: 11
I had the same problem and to solve it I had to eliminate these first lines of code that are commented because it was executed before time. By eliminating those lines of code it worked perfectly for me I hope it works for you.
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
/*
if (userDefaults.bool(forKey: KeysUserDefaults.UsuarioLocal.esUsuario) == false)
{
let sb = UIStoryboard(name: "FirstPage", bundle: nil)
if let vc = sb.instantiateViewController(identifier: "FirstSB") as? FirstPageViewController
{
self.present(vc, animated: true, completion: nil)
}
}
*/
// Intentamos iniciar sesion con los datos de userDefaults
if (userDefaults.string(forKey: KeysUserDefaults.idToken) != "" && userDefaults.string(forKey: KeysUserDefaults.accessToken) != "")
{
let credential = GoogleAuthProvider.credential(withIDToken: userDefaults.string(forKey: KeysUserDefaults.idToken)!, accessToken: userDefaults.string(forKey: KeysUserDefaults.accessToken)!)
Auth.auth().signIn(with: credential)
{ (authResult, error) in
if error != nil
{
print("Error al intentar iniciar sesion de entrada")
print(error?.localizedDescription ?? "")
// Mandamos al usuario a la pagina de Login/SignIn
self.aRegistrarse()
}else
{
// El usuario se cargo con exito.
print("Nombre: \(authResult!.user.displayName!)")
}
}
}else
{
print("Credencial del usuario vacia.")
// Mandamos al usuario a la pagina de Login/SignIn
self.aRegistrarse()
}
}
Upvotes: 0