Wednesday, August 7, 2013

iOS saving data to user defaults and getting data from user defaults.

Today I’m going to talk about a method to save application specific or user specific data in iOS, which is called “user defaults”. We can do this by using NSUserDefaults class. You can save objects of following types.

NSString, NSNumber, NSDate, NSData, NSArray, NSDictionary

For more information see the apple documentation from here.

The data saved in the user defaults is available until you delete the application.

You can use user defaults to save user specific or application specific stetting’s such as last state of the application, user color theme, user session and etc.

I’m going to give a small example about how to save data to user defaults and how to read data from user defaults. I’m going to save my application current user details (user first name, last name, image url, and user id) to user defaults.

Save current user details to user defaults .



Get user details from user defaults.



If you want to save any other type of data such as UIImage you have to convert it into NSData and save it.

No comments:

Post a Comment