Save small data in iOS / Objective C++

Save small data in iOS / Objective C++

In some development cases you might need to save data on your iPhone app permanently. But you may no need to design a database for it as well. In this case you can use below code snippets.

To save data you can use below code.
On the below code "DasunTharaka" is the value of data and "name" is the data accessing key.


 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 [prefs setObject:@"DasunTharaka" forKey:@"name"];

Then you can read them using below code.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *textToLoad = [prefs stringForKey:@"name"];
_myTxt.text = textToLoad;

Also this data will not be erased even if you kill the app or switched off the phone.

Happy coding.
Thanks.
Dasun


Comments

Popular Posts