How to keep the user logged in, in Swift (iOS)? - ios

I am making an app that initially asks the user to log in, and on successful login.. takes the user to their dashboard. But, now, when I close the app.. I want the user to be logged in and the app to directly open from their dashboard, and not the login page. How do I do it?
Please see the link (gif), as I'm not able to show it here because it's my first question on stackoverflow
P.S. I'm new to iOS, I did surf internet and found something "UserDefaults".. but everything on it, included the use of navigation controller in the storyboard.. I'm not using any navigation controller.. but simply using a "show" segue for transition to the user dashboard.
My storyboard image, segue.
Please help!
Thanks.

The use of a navigation controller does not affect the user defaults. Anywhere in the app, you can call
let defaults = UserDefaults.standard
You can then write to defaults by calling
defaults.setValue(value, forKey: "key")
And later retrieve this value by doing
let savedValue = default.value(forKey: "key")
Depending on what you're using for login, you will need to decide what needs to be saved and retrieved, and where and when that needs to happen

I would agree with using UserDefaults, however, UserDefaults are easily accessible and are not safe. You would need to find a secure solution in case you are storing sensitive data (such as token). CoreData would be an option if you want to store data securely.
A third option would be to store the token in Keychain. This is also a secure method. The advantage is that the same token can be used by all of your apps (by the same developer) and the token persists even if the app is removed/re-installed.

Related

The Right Way To Share Data Got From Server Across all View Controllers?

I'm working on the Authentication Part of my app flow. So in the Login View Controller when the user taps the Login Button, network requests for the UserId. If a UserId is verified, then the user can Login. The Thing is all view controllers in my application are going to make network requests with that UserId , so I need a way to make the UserId accessible to all view controllers . How do I achieve that ?
Also what am I doing wrong? How would a Pro-Developer go with this situation ? Thanks for any help. Please also you can also share online resources. Thanks !!
It all depends on your requirement like:
If you don't want to persist UserId after app kill then you can use Singleton Class
If you want to persist UserId after app kill and you are not so concern about the security then can use UserDefault
If you want to persist UserId after app kill and you also wanted to follow guidelines to save sensitive data then can use Keychain
There are more options to use like Database FileWrite, so you need to choose wisely based upon your requirement.
You can find tons of tutorials for all above points
You can save the userID & other User details you want to save through out the app by saving these in NSUserDefaults
var userID = "user1"
UserDefaults.standard.set(userID, forKey: "userID")
Now , whenever & In whichever screen you want to access these details, you can access like this.
if let userId = UserDefaults.standard.string(forKey: "userID") {
// use details here...
}

Bypass login if user already created account

the reason I am asking this question is because I am not using firebase or parse to create accounts. I have used my own code to use CloudKit to login and so on. My question is, after the first time opening the app, and the user making an account, how can I make sure that the following time they open the app, it bypasses the login page? And then, if they logout, the next time they open the app, it takes them to the login page. How can this be done.
TL;DR how can I track whether or not a user has been logged in, and then open the app to a 2 possible scenes based on whether or not they are or are not logged in.
Firstly, you could keep the user sign in data (like a returned authkey, or a boolean flag) in the Defaults for the app.
Secondly, you could make a "Loading View" where you would check if that data is present.
Lastly, if the data isn't present move him to the login view, else to the main app.

Swift - How to implement a login/session (no code)

My application has a login page and i would like to keep the user logged-in when he closes the app. Also, i have a logout button which should log-out the user and display the login page (even when he closes the app).
I tried to implement this using core data, but I have some issues with that and i'm not sure that is the best way to do it.
Can someone please give me some advices? I don't need code but just some idea of how i can implement that please.
Thanks!
You can make it as simple or complicated as you like. As simple as a Bool value in NSUserDefaults would do it. You could persist state in CoreData, Filesystem, NSUserDefaults. We store login credentials securely in the KeyChain.
For the authenticated areas of the app, check the state you have persisted on next app launch.
On logout, remove the state you have stored.

How can all of my ViewControllers for iOS have user login information available to them?

I'm not sure what the correct pattern for this situation is, so I'm not really sure what to search for here...
Basically, I want to have my iOS (iPhone) application start with a login page always. From there, I need the rest of the application's ViewControllers to be able to access the login information, and I'm not sure what the best way to do this is...
Do I create some form of global variable somehow?
Do I pass in an instance of the class to each ViewController and have a property for each ViewController to hold this login information?
Do I store some token in the cache/temp/appfolder somehow that each one can read from to get login information?
You could go with a shared instance of an object that holds the users credentials. You could also put it in a property or object in your app delegate or you could write it to disk (either NSUserDefaults or a special file).
For sensitive login credentials however, the recommended thing would be to use the keychain for persistence.
I think I would go with a combination of keychain for persisting the data and a shared instance or class method to retrieve it throughout the app.
There is concept called NSUserDefaults in iOS to which is used to store light weight information in iPhone memory.
Look at this examples to learn storing and retrieving data in NSUserDefaults : example1 and example2.
EDIT :
As you want to store login and password, use keychain. Here is an keychain example.
You can use NSUserDefaults to store the username/password, but if you just need the login credentials or a token to make new service calls, and you usually send them on the headers of an HTTP request, I would just set them on the headers of your service classes, so you don't really need to store it anywhere.
If you want to store the info from a login response (like name, email, etc), I usually have a Constant class in my projects to keep global references and other constants (I don't like to keep stuff in the AppDelegate and have it included on every class), and I have a User class with all the user info. I keep a reference of that User object on the Constant class. It's simpler than having to pass it down to every ViewController in your app.
This is a simple, yet useful approach:
Use NSUserDefaults for storing a BOOL named didAuthUser
Whenever you want show a view controller that needs authentication check for didAuthUser (this is usually the root view controller) like this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:#"didAuthUser"]) {
//Get stored credentials
}else{
//Show login view controller
}
So, when a user starts using yout app, you'll be able to show a log in view controller. If the user has already logged in, you must check the stored credentials (use the keychain, do not store sensitive information in NSUserDefaults)
Using the keychain is not a very straightforward process, you may want to use something like this
If you use sskeychain, a simple implementation would use the following methods:
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
Note that the method for retrieving the password asks for an account, you may use NSUserDefaults for this. It's ok to store the account on NSUserDefaults as long as the password is secure on the keychain.
This way you can access the login data all across the app.
Happy coding!

iOS Twitter+OAuth check if user is logged in

I'm developing an app in which I want to give the user the option to be logged in with Twitter. My problem is that I want to check if the user is logged before given access to certain functions. Say I have a view and I want to show different content depending on the users is logged in or not. I know I can log the user in when opening the app, but I don't want to show the login screen every time if the user choose not to log in. (I'm using the Twitter+OAuth/MGTwitterEngine framework).
How can I set up a control like that? Any tips is appreciated!
Seems like you would want to save the authorization token in NSUserDefaults. Then, on launch, you would check for that token
if (![[NSUserDefaults standardUserDefaults] objectForKey:#"whatever_you_call_your_auth_token") {
//send the user to twitter login
} else {
//set isLoggedInViaTwitter:YES
}
So if you have a boolean value like isLoggedInViaTwitter, and you set that to YES or NO based on whether the auth token is present in NSUserDefaults, you can use the value of that to determine what content to present in your views.
I'm new but I hope this helps to some extent. If I've misunderstood your question, please let me know.

Resources