title: I implemented a google sign in in ios app. Now, how can I verify if user is logged in and if not - return him to the login page with Swift?
body: <p>As I wrote in the title of this question - I went through this tutorial <a href="https://developers.google.com/identity/sign-in/ios/sign-in" rel="noreferrer">https://developers.google.com/identity/sign-in/ios/sign-in</a> and now I'm able to log in the user to my app based on his google credentials. </p>
  
  <p>The way I do it so far is that I have a <code>ViewController.swift</code> class with the following code:</p>
  
  <pre><code>class ViewController: UIViewController, GIDSignInUIDelegate {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          let background = CAGradientLayer().greenBlue()
          background.frame = self.view.bounds
          self.view.layer.insertSublayer(background, atIndex: 0)
          //GIDSignIn.sharedInstance().uiDelegate = self
  
          // Uncomment to automatically sign in the user.
          //GIDSignIn.sharedInstance().signInSilently()
  
      }
  
      override func viewDidAppear(animated: Bool) {
          super.viewDidAppear(animated)
          GIDSignIn.sharedInstance().uiDelegate = self
  
          GIDSignIn.sharedInstance().signInSilently()
      }
  
      @IBAction func didTapSignOut(sender: AnyObject) {
          GIDSignIn.sharedInstance().signOut()
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      func signInWillDispatch(signIn: GIDSignIn!, error: NSError!) {
          print("Nothing!")
      }
  
      // Present a view that prompts the user to sign in with Google
      func signIn(signIn: GIDSignIn!,
          presentViewController viewController: UIViewController!) {
              self.presentViewController(viewController, animated: true, completion: nil)
      }
  
      // Dismiss the "Sign in with Google" view
      func signIn(signIn: GIDSignIn!,
          dismissViewController viewController: UIViewController!) {
              self.dismissViewControllerAnimated(true, completion: nil)
      }
  }
  </code></pre>
  
  <p>and in my AppDelegate.swift class I have:</p>
  
  <pre><code>class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
  
      var window: UIWindow?
  
  
      func application(application: UIApplication,
          didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
              // Initialize sign-in
              var configureError: NSError?
              GGLContext.sharedInstance().configureWithError(&amp;configureError)
              assert(configureError == nil, "Error configuring Google services: \(configureError)")
  
              GIDSignIn.sharedInstance().delegate = self
  
              return true
      }
  
      // [START openurl]
      func application(application: UIApplication,
          openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -&gt; Bool {
              return GIDSignIn.sharedInstance().handleURL(url,
                  sourceApplication: sourceApplication,
                  annotation: annotation)
      }
      // [END openurl]
  
      @available(iOS 9.0, *)
      func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -&gt; Bool {
          return GIDSignIn.sharedInstance().handleURL(url,
              sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String?,
              annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
      }
  
      // [START signin_handler]
      func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
          withError error: NSError!) {
              if (error == nil) {
                  print("Signed in!")
              } else {
                  print("\(error.localizedDescription)")
  
              }
      }
      // [END signin_handler]
  
      // [START disconnect_handler]
      func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
          withError error: NSError!) {
              // Perform any operations when the user disconnects from app here.
              // [START_EXCLUDE]
              NSNotificationCenter.defaultCenter().postNotificationName(
                  "ToggleAuthUINotification",
                  object: nil,
                  userInfo: ["statusText": "User has disconnected."])
              // [END_EXCLUDE]
      }
  }
  </code></pre>
  
  <p>My storyboard looks as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/dxzAO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dxzAO.png" alt="enter image description here"></a></p>
  
  <p>On the left I have a <code>ViewController</code> with marked google button (which is white, therefore it's not visible here - sorry!) and on the right I have a main <code>TabController</code> view that contains the whole logic of my app (so far it's quite empty):</p>
  
  <pre><code>class TabController: UITabBarController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          let background = CAGradientLayer().greenBlue()
          background.frame = self.view.bounds
          self.view.layer.insertSublayer(background, atIndex: 0)
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  }
  </code></pre>
  
  <p>Now it works like this:</p>
  
  <p>I run the app and see the google button to sign in. I sign in and when everything is validated - nothing changes, I'm not moving to the second screen (<code>TabController</code>). I just stay on this view and I can keep clicking the google button - nothing changes though because I'm already signed in. </p>
  
  <p>I want to achieve a situation when the user opens my app and when he is not logged in - he sees the <code>ViewController</code> screen. When he logs in - he sees the <code>TabController</code> screen. And also when he already signed in before and opens my app - he immediately jumps to <code>TabController</code> and skips the <code>ViewController</code> page. How can I achieve it? </p>
  
  <p>I suspect I have to mark on my storyboard my <code>TabController</code> to be the <code>initial view controller</code>, but what about the log in screen?</p>
  
  <p>=====EDIT</p>
  
  <p>following Mac Bellingrath answer I modified my function in <code>appDelegate.swift</code> class, now it looks like this:</p>
  
  <pre><code>func application(application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
          // Initialize sign-in
          var configureError: NSError?
          GGLContext.sharedInstance().configureWithError(&amp;configureError)
          assert(configureError == nil, "Error configuring Google services: \(configureError)")
  
          GIDSignIn.sharedInstance().delegate = self
  
          /* check for user's token */
          if GIDSignIn.sharedInstance().hasAuthInKeychain() {
              /* Code to show your tab bar controller */
              print("user is signed in")
              let sb = UIStoryboard(name: "Main", bundle: nil)
              if let tabBarVC = sb.instantiateViewControllerWithIdentifier("TabController") as? UITabBarController {
                  window!.rootViewController = tabBarVC
              }
          } else {
              print("user is NOT signed in")
              /* code to show your login VC */
              let sb = UIStoryboard(name: "Main", bundle: nil)
              if let tabBarVC = sb.instantiateViewControllerWithIdentifier("ViewController") as? ViewController {
                  window!.rootViewController = tabBarVC
              }
          }
  
          return true
  }
  </code></pre>
  
  <p>Now when I run the app and the user has been signed in before - I see the <code>tabController</code> view. If he wasn't signed in before - I see the <code>ViewController</code> view. It works almost like a charm, but I realized that no matter if I write <code>if GIDSignIn.sharedInstance().hasAuthInKeychain() {</code> or <code>if !GIDSignIn.sharedInstance().hasAuthInKeychain() {</code> it always prints out 
  the message <code>user is signed in</code>, so something is still not right...</p>