0

I've successfully integrated Google Sign-In into my iOS app in my AppDelegate.swift file, and can successfully detect a successful sign-in (by printing "success" out onto the console). The issue is that after a successful sign-in, I'm back at the Google Sign-In page when I want to be in the next screen of the app.

The AppDelegate.swift file did not recognize the performSegue function, as it is a function of the UIViewController class (please correct me if I'm wrong). To get around this, I created a global variable in the ViewController file such that the segue would be performed whenever this value would be changed:

AppDelegate.swift:

var userSignedInGlobal = "n/a"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    // A bunch of code that implements the Google Sign in ...

    print("Successfully logged into Google.", user)
    userSignedInGlobal = "success"

}

And then my InitialViewController.swift file:

class InitialViewController: UIViewController, GIDSignInUIDelegate  {

    var userSignedIn = userSignedInGlobal {
        didSet {
            performSegue(withIdentifier: "segueOne", sender: self)
        }
    }

    // A bunch of irrelevant code.

}

This did not work, and the reason I think this didn't work is that userSignedInGlobal in the InitialViewController.swift file is being passed by reference - so even when its value changes, the value of userSignedIn does not change (again, please do correct me if I'm wrong).

To get around this, I changed my InitialViewController.swift file as follows:

var userSignedIn = userSignedInGlobal {
    didSet {
        doSegue()
    }
}

class InitialViewController: UIViewController, GIDSignInUIDelegate  {

    func doSegue() {
        performSegue(withIdentifier: "segueOne", sender: self)
    }

    // A bunch of irrelevant code.

}

This gave me an error in the third line: "Use of unresolved identifier 'doSegue()' "

I do not know how to go about performing the segue when the sign in is successful. Any help will be greatly appreciated, thanks in advance.

1 Answers1

3

There are a few problems here:

  1. Global Variables in App Delegate

There are numerous answers demonstrating how to store a global variable in your AppDelegate and then subsequently reference that variable in your view controllers, e.g. getting a reference to app delegate

  1. Perform Segue only from Storyboard based controllers

You won't be able to use performSegue unless your current View Controller was loaded from a StoryBoard (see performSegue). This probably is not the case with whatever Google view you are loading from your AppDelegate.

  1. Are you doing the login in the right place?

You might want to reconsider the division of duties between your AppDelegate and your initial view controller. Perhaps you are trying to do too much in the AppDelegate. Perhaps you should move some of this code (showing the Google form) to your initial VC and then either segue or reset your Navigation Controller based on the result.

Hope this helps.

ozzieozumo
  • 426
  • 2
  • 8
  • PS: note that it would be ok to have your initialVC be the GIDSignInDelegate and the GIDSignInUIDelegate. That simplifies things and you can find plenty of examples using that approach. However, if you want to have your AppDelegate act as the GIDSignInDelegate and your VC act as the GIDSignInUIDelegate (as indicated in your code fragment), then you probably need to look at using NotificationCenter to pass a message from the signIn func of your AppDelegate, to your VC. The observer (which would be in your VC) could then do a performSegue or whatever. – ozzieozumo Oct 16 '17 at 01:18
  • Thanks for your response! I tried moving the Google Sign In code to my VC, but for some reason, the function was never called...I'm very confused as to why. Do you have any ideas? – Arjun Gupta Oct 28 '17 at 04:10