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?

According to Google's documentation, it appears that you can use GIDSignIn's instance method hasAuthInKeychain() to check whether the user has either currently signed in or has previous authentication saved in keychain.

So, in your AppDelegate's

func application(application: UIApplication,didFinishLaunchingWithOptions                    launchOptions: [NSObject: AnyObject]?) -> Bool {

You might write something like:

GIDSignIn.sharedInstance().delegate = self
/* check for user's token */
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
/* Code to show your tab bar controller */
} else {
/* code to show your login VC */
}

for example if our user is signed in:

let sb = UIStoryboard(name: "Main", bundle: nil)
if let tabBarVC = sb.instantiateViewControllerWithIdentifier("MainTabBarVC") as? UITabBarController {
window.rootViewController = tabBarVC
}

*Substitute your UITabBarController subclass for UITabBarController.

Of course, there are many ways to change the flow. Check out storyboard reference and multiple storyboards.


let googleUser = GIDSignIn.sharedInstance().isCurrentUser

if googleUser != nil {
// Get new token from google and send to server
let strToken = googleUser.authentication.idToken
} else {
// present login screen here
presentLoginScreen()
}