How to remove all NSUser Defaults except two objects? - objective-c

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.

Related

Deleting App from MacOS but not cleared NSUserDefaults data

I have one.dmg file, installed it one time and some data saved in NSUserDefaults. But When I am remove from application as well Trash its not cleared of that NSUserDefaults. I want to remove all of that.
You use the code in applicationWillTerminate callback:
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
https://stackoverflow.com/a/29653178/2837760

Switching between NSUserDefaults

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.

Retrieve saved (NSUserDefaults) date to another class

[[NSUserDefaults standardUserDefaults] setObject:datePicker.date forKey:#"birthDate"];
[[NSUserDefaults standardUserDefaults] setInteger:sexSegmented.selectedSegmentIndex forKey:#"sex"];
[[NSUserDefaults standardUserDefaults] synchronize];
This is the value that I am saving to userdefaults and from another class I want to call them.
NSDate *birthDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"birthDate"];
NSInteger a=[[NSUserDefaults standardUserDefaults] integerForKey:#"sex"];
NSLog(#"%#",birthDate);
[datePicker setDate:birthDate animated:YES];
sexSegmented.selectedSegmentIndex=a;
This is the other class from where I am getting the saved values, but I can't see any value in birthDate. I want to take it and set the new datePicker to birthDate. Can anyone help me?
It's most likely a case of the interface outlet of datePicker not being connected properly. This would make datePicker nil and as a result you would end up storing nil in the user defaults.
You can validate this by doing
NSLog(#"%#", datePicker);
before your first snippet of code.

How to fetch the value saved in NSUserdefault

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...

Save Username & Pass Objective C iPhone

i have a textfield,
Number and Password
i have no clue how to save these settings and then read them as the app starts to check if they have been set or not.
Thanks
Mason
Sensitive information should be stored in the keychain, a cryptographically secure location on the device. If you save the username and/or password in NSUserDefaults, you're saving them as plaintext, which is inherently insecure.
There're are plenty of examples on the internet of how to use the keychain on the iPhone, include simple wrappers to use in your code. For example, here's some pretty good code on Github that makes it quite easy:
http://github.com/ldandersen/scifihifi-iphone/tree/master/security
To Save:
[[NSUserDefaults standardUserDefaults] setValue:_email forKey:#"email"];
[[NSUserDefaults standardUserDefaults] setValue:_password forKey:#"password"];
[[NSUserDefaults standardUserDefaults] synchronize];
To Read:
_email = [[NSUserDefaults standardUserDefaults] stringForKey:#"email"];
_password = [[NSUserDefaults standardUserDefaults] stringForKey:#"password"];
In your case:
[[NSUserDefaults standardUserDefaults] setValue:Number.text forKey:#"Number"];
And:
NSString * number = [[NSUserDefaults standardUserDefaults] stringForKey:#"Number"];
The "Key" is usually hard-coded and works like a "variable name" for things in storage (typical name/value pairs like a dictionary).
To check if the value has been set or not; after you read it:
if (number) ...