Login persistence between app launches.
Most apps that require a user to register and be logged in when using the app implements login persistence. Login persistence enables the user to not have to enter their login credentials every time they use the app. A user simply registers and logs in the first time they use the app. On the subsequent launches, the app remembers which user was using the device and automatically logs the user in.
So far, we haven’t talked about how save any information on the device itself. Any information in our app comes straight from our server or is entered by the users themselves.
iOS provides us with a class called NSUserDefaults. This is a singleton class. A singleton class is able to instantiate only a single object. Detailed information about Singleton classes or NSUserDefaults is beyond the scope of this post.
We use NSUserDefaults and save some basic information about our user, for instance we can choose to save the user’s id, a URL string for their profile picture etc. At the moment it is too soon, to list all the information we shall use NSUserDefaults to persist. We just simply use the objectForKey: method to save the above mentioned information in the NSUserDefaults object the same way we would be storing it in a NSDictionary. After that synchronize in order to save this information in permanent store.
In our above example, the next time the app launches, we may choose to use the user id to identify the user, thus skipping the login process to bring the main content of our app faster.
NSUserDefaults is mainly for storing small amounts of data. If we wanted to persist big data, then we would probably do something like write them out to disk. A more detailed post is likely to follow in the future if we decide to implement something like that.