bloemy bloemy - 1 year ago 103
iOS Question

Xcode iOS: check if user is logged in and show different views if not

I'm coding an app where a logged in user has got a couple of extra functionalities than a non logged in user. Basically, I have more or less 5 tabs. When I launch the app, the user immediately gets the login page. He can decide to skip it. If he skips it, there'll only be 3 tabs for him. If he logs in successfully, there'll be 5.

I already have my login page made. I just don't know how I can store a session if the user is logged in correctly, and only display a certain number of tabs if the user isn't. I come from PHP, I've just started learning Objective-C, so I'm looking for the same thing as $_SESSION in PHP, more or less.

So: if user logs in, store session, and show all the tabs. If he doesn't, only show a limited number of tabs.
How should I approach this?

Answer Source

In terms of storing the session, I assume username and password is enough.

You could store the username as you wish in NSUserDefaults or CoreData if you are using it. Storing a password is best using the keychain. SSKeychain makes it easy to do this.

[SSKeychain setPassword:password forService:myAppName account:userName]

You could store the fact they are logged in in-memory, but on app relaunch check by:

NSString *password = [SSKeychain passwordForService:myAppName account:userName];
if (password != nil)
    // Logged in

If the user logs out, easy as deleting the password from the keychain by

[SSKeychain deletePasswordForService:myAppName account:userName]