Store two user defined strings in iOS - ios

I am new to iOS programming - I have written an iOS app for a company that uses the app for their workers to log in when they go to work, and to log out when they leave. (They often work on remote places).
All I need now is a way to store two simple strings.
1. String is the user name
2. String is a salted md5 password
Then the user doesn't need to write all the credentials every time he wants to login and out.
What is the simples? SQLite, CoreData, Plst?
Thanks!

See my answer here:
I'd avoid using a plist. The easiest way to save simple data in an application, by far, is NSUserDefaults.
Check out this tutorial for a simple guide on how to use NSUserDefaults. Always be sure to synchronize NSUserDefaults when you're done writing to them.
Example:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// Storing an NSString:
NSString *user = #"MyUsername";
[prefs setObject:user forKey:#"username"];
[prefs synchronize];
Later:
// Retrieving an NSString:
NSString *savedUserName = [prefs stringForKey:#"username"];

Related

App will load data stored in user defaults into memory at app launch?

I recently found that when I save, for example, 10MB data into user defaults and I relaunch app, the app's memory is larger about 10MB than previous launch according to Xcode memory report.
So I can't use NSUserDefaults to save large data for a good performance?
And the data is email messages, have notion of folder (inbox, trash, etc) and the messages' attachments need save to local. I know SQLite and I use it to store data that need for search, but it's some complex, I don't know whether CoreData is a good choice.
I planned to store emails to NSUserDefaults because it's very simple for I just implement NSCoding protocol, but now it seems not good solution for the memory issue.
// Save
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mailFolders];
[userDefaults setObject:data forKey:#"myKey"];
[userDefaults synchronize];
// Read
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *mailData = [userDefaults objectForKey:#"myKey"];
NSArray *mailDataArray = [NSKeyedUnarchiver unarchiveObjectWithData:mailData];
NSLog(#"mail data size:%#", [NSByteCountFormatter stringFromByteCount:mailData.length countStyle:NSByteCountFormatterCountStyleFile]);
/**
mailFolder {
folderInfo,
messages
}
*/
i thnik there is no size limit to storing in NSUserDefaults.It's all upon device storage..
you can check this link
https://discussions.apple.com/thread/1763096?start=0&tstart=0
property lists should be used for data that consists primarily of strings and numbers. They are very inefficient when used with large blocks of binary data.
Tom,
There are many way in iOS to store the data.
1- NSUserDefault
2- Plist file
3- Data Base
If you have some small data then, NSUserDefaults and Plist is best option (no need to create database).
But if you have a large amount of data then, i would suggest you to use a proper DataBase (Sqlite OR CoreData).
NSUserDefaults is really meant for storing small pieces of data such as settings, preferences, and individual values.
Suggest using Core Data or SQLite to store a large list of elements.
There was a very good question about sqlite vs. Core Data;
Working with data in iOS Apps and Core Data vs SQLite 3 on Stack Overflow, you may want to read through the answers to that question.

Save values from the first launch and I want to keep using that value. How can I do this?

I'm developing an app that gets identification number and age at the first launch.
I also want to use these values later on. Where should I save these values in?
Is it automatically saved?
My language is Swift.
You can save them in NSUserDefaults,
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:#"value:#"key"];
[def synchronize];

Recomendation on storing data in share preference in iPhone

I am new in share preference so kindly forgive me this silly question. I am developing an application in which I have to fetch data from xml and all data are in text form, So I think I'll store this all data into shared preference because this is quite light weighted and not too much so thats why not going for sqlite. So before jumping into this approach I want to know is that good approach or not and what are recommendations on storing data in share preference?
To store data:
NSString *someValue = #"someval";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:someValue forKey:#"nameOfSomeValue"];
[defaults synchronize];
To retrieve it later:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *someValue = [defaults valueForKey:#"nameOfSomeValue"];
SharedPreferences is an Android thing, and your question seems to be about iOS.
You can store user preferences and other lightweight data in NSUserDefaults on iOS, as described in my answer here.
If you want to store data in XML files, this answer provides a helpful example.

If once value assigned to a variable in objective c while application install then how to access that value at every time of opening an application?

Hi i am new to objective C.
If once value assigned to a variable in objective c while application install then how to access that value at every time of opening an application? And i tried with extern, static. Each will assign and sets the value at first time. If i rerun the application in emulator, it is not taking that last assigned value.
If anything is possible other than File system storage ? Is it possible with static or extern variables.
Scenario:
While install "extern int test" is assigned to 10 then it changed to 20. While accessing from another class, test reflects 20. If i rerun the app, "test" is showing "10"
But i want to access the last assigned the value (like static in java)
Thanks for any help !
Use NSUserDefaults to set the value in AppDelegate :
NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:20 forKey:#"abc"];
[defaults synchronize];
TO get the value again, Use this method :
NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
var = [defaults integerForKey:#"abc"];
After closing the app the data stored in variables do not persist (of all classes), so you need to use NSUserDefaults (in your case)
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs.
At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database.
user's defaults database persists when app is killed.
Use NSUSerDefaults value will remain saved until application is deleted
[[NSUserDefaults standardUserDefaults] setValue:obj1 forKey:key1];
[[NSUserDefaults standardUserDefaults] synchronize];
and access value like
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id obj = [defaults objectForKey:key1];
Because all variables of Objective C program are stored in memory, none of them survives application termination. If you need the value to be available after your app restarts, you have several options:
Use the file system - this one is the most straightforward thing to do. Cocoa provides APIs for writing strings to files in a single go, so your code would be short and simple.
Use user defaults - This API helps you persist values for reuse in an organized way.
Use keychain APIs - This API lets you save small amounts of data that must be encrypted. This is probably an overkill in your scenario.
All the variables are store in Memory once will restart your application the memory is released by ARC and allocated by new one memory address
Can user FileSystem or NSUserDefaults in objective c

Is it possible to not back up some of the files saved in nsdefault?

I used NSDefault in my app to backup some images and got rejected because it uses 6mb of storage.
Can anyone help me add the donotbackup attribute into it? I would like to keep userdefault directory if possible so old users don't lose their images. Any help would be really appreciated :)
My current code is:
to save:
- (IBAction)d1p:(id)sender {
lbl1.text=txt1.text; [txt1 setAlpha:0];
NSString *savestringln1 = lbl1.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestringln1 forKey:#"savedstringlbl1"];[defaults synchronize];
[self.view endEditing:YES];
}
To load:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstringlbl1 = [defaults objectForKey:#"savedstringlbl1"];
So you want to back up images? NSUserDefaults isn't really meant for that.
Why not try adding iCloud support instead?
The only way to stop something from not being stored in UserDefaults is to just not put it in User Defaults, I am not sure what you mean by backup, you are just duplicating images on the device in two different formats. If the image is on your file system you can store the path to the image in User Defaults if thats useful to you. If you are trying to prevent unintentional deletion can you just mark the 'deleted' images has hidden from the user in some way, a flag, moved to a different location perhaps. You also might want to look into CoreData that can deal with larger amounts of data ad do things like have non serialised properties and such, but that is still just storing your data on the device.

Resources