I am trying to use NSUser Deafault in my application user have tp fillup 4 UITextFields. I am trying to save the fields so that on subsequent runs of the program that whatever they previously put will already be displayed in those UITextFields so the wont have to re-input it in - unless they want to edit something in which case they still have that option. I think that I have figured out a good way to save the strings using NSUserDefaults but now I am trying to figure out how to have those fields populate a UITextField - it doesnt seem as easy as if they were UILabels. This is the route I am attempting:
Now my value is being saved in nsuserDefalut,but hot how to fetch dat value next time(when i re start application (null appear on the uitext field)) ..I am trying this cods
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults]; //Hooks.
NSString *placeHolderName = [userData stringForKey:#"name"];
urltextFieldNormal.text = placeHolderName;
Do you remember to sync NSUserDefaults when you save the strings?
[[NSUserDefaults standardUserDefaults] setObject:#"Hello mister nick" forKey:#"alarmsArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
Cheers mate...
Related
I am using a lot NSUserDefaults and i want every time the app starts to remove them except two objects is this somehow possible?
Any help appreciated
I assume you know which two objects you want to keep? If that's the case use this code:
id obj1 = [[NSUserDefaults standardUserDefaults] objectForKey#"keyForObj1"];
id obj2 = [[NSUserDefaults standardUserDefaults] objectForKey#"keyForObj2"];
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
[[NSUserDefaults standardUserDefaults] setObject:obj1 forKey:#"keyForObj1"];
[[NSUserDefaults standardUserDefaults] setObject:obj2 forKey:#"keyForObj2"];
You would need to keep track of the keys yourself and remove the objects associated with them on launch. More importantly, though: why are you storing data in NSUserDefaults that you only want to persist through a single run of the app? That sort of data should probably be kept in memory.
You can save Those two items under separate a key and Add into the Code during ViewDidLoad.
Let me know the Actual Problem.
I have a check box type application, kinda like notes on the iPhone. When the user closes the application and double tabs the home button and deletes the application from the background, the things they have typed also get deleted or reset. I was thinking of calling
[self saveContext];
But that didn't work. WHat should I be calling?
You can usual use something such as NSUserDefaults to save information. Or, you can go another route and save the information in a data structure and write that to a file. There are many easy ways of doing this. For instance, if you want to preserve a checked value, you could do the following:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:"checked"];
[defaults synchronize];
Very simple, and then can be queried across app exits, like so:
if( [defaults boolForKey:"checked"] ) {
//do stuff here
}
i have been working on a app, and needed to store a string. I used this code to set the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:CertificateKey.stringValue forKey:#"SavedKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#",[defaults objectForKey:#"SavedKey"]);
I loged it, so i know it saved...well, it showed me the value.
When i open my application, I use this to retrieve the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[CertificateKey setStringValue:[defaults objectForKey:#"SavedKey"]];
[CertificateKey setTitleWithMnemonic:[defaults objectForKey:#"SavedKey"]];
[[NSUserDefaults standardUserDefaults] synchronize];
Why will it not get the default value? Did i not completely save it?
Don't quit the application by pressing the stop button in xcode. Instead, quit it by right clicking on the application icon and selecting "Quit".
Edit
Maybe the first time that you execute the application, you want to save some defaults but you don't want to set them the second+ time that the application runs.
For this purpose in some class initialize method register the defaults, like this:
+ (void) initialize
{
NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
[defaults registerDefaults: #{} ];
// This case it's an empty dictionary, but you can put whatever you want inside it.
// just convert it to data if it's not an object storable to a plist file.
}
Also, you're using setValue:forKey: , that method is inherited from NSObject. Use setObject:forKey: .
And use finalize if you want to save the defaults at the end:
- (void) finalize
{
// Save the defaults here
}
You might have a problem because you are creating an instance of NSUserDefaults. From what I understand you are supposed to access it like so: [[NSUserDefaults standardUserDefaults] setValue:#"Pikachu" forKey:#"Best Pokemon Ever"]; [[NSUserDefaults standardDefaults] objectForKey:#"Best Pokemon Ever"]; Rather than actually creating an instance of it.
Maybe somebody will know, how to "tag" NSuserdefaults.
I have like 10 user defaults:
name
pin
etc...
I want to make another object, which would have the same defaults as the other ones (same variables but different values). Like a version, if object == 1 then load one userdefaults and if object == 0 another ones. But how to make it done?
Tried to make something like this
[NSUserDefaults setVersion:object.intValue];
But i guess this isn't the way to do it. So maybe anyone could help how to make it done?
To be more specific a simple example would help like how to do this:
Object (1 or 0)
[[NSuserDefaults standartUserDefaults] setObject: #"something"
forKey: #"Name"];
[[NSUserDefaults standartUserDefaults] synchronize];
NSString *name = [[NSUserDefaults standartUserDefaults] stringForKey:"#name"];
How to set and get this Name depending on Object value?
You could use persistent domains.
NSMutableDictionary *defaults = [[[NSUserDefaults standardUserDefaults] persistantDomainForName:#"aString"] mutableCopy];
// make changes to the dictionary here
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:#"aString"];
[[NSUserDefaults standardUserDefaults] setPersistantDomain:defaults forName:#"aString"];
Note that you'll need to be accessing all the defaults via the dictionary if you do this.
I'm having trouble getting some informations written in my .plist, and NSUser Defaults Files.
For the .plist i've already posted but, nothing seems to be wrong:
How to update an array set into the .plist dictionary
And for the NSUser Default, i'm doing something like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *userSettings = [[NSMutableArray alloc]initWithArray:[[defaults objectForKey:#"userSettings"]mutableCopy]];
[userSettings addObject:userSettingsArray];//adding an array into my userSettingsArray (declared higher in the code)
[defaults setValue:userSettings forKey:#"userSettings"];
[defaults synchronize];
I was wandering, is there any option in Xcode allowing , or not, the user to write into the app folders?
I'm a little stuck here
Thank you for your help,
Tim
Use
[defaults setObject:userSettings forKey:#"userSettings"];
instead of
[defaults setValue:userSettings forKey:#"userSettings"];
I found the solution to my problem:
I entered the datas from the IBOutlet like this
[self.tempUserSettings insertObject:self.firstName atIndex:1];
instead of this:
[self.tempUserSettings insertObject:self.firstName.text atIndex:1];
so the datas were wrong, and so and so.....
Thank you for you help, learned a lot from those .plist and NSUserDefault