Save Username & Pass Objective C iPhone - objective-c

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

Related

Data persistance in cocoa for small amount of data

I'm busy writing my first cocoa application and I have to store 2 NSStrings somewhere on the hard disc (an username and password). What would be the best way to do this? Just write to a file? And if so, where would be the best place for me to store this file?
I just don't want to use some overkill technique for just 2 simple Strings.. Thanks!
The easiest is using UserDefaults. It's a place usually used to store application's settings and preferences.
To store a string
NSString *user = #"Peter";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:#"userName"];
to get it back later
NSString *user = [[NSUserDefaults standardUserDefaults] stringForKey:#"userName"];
If you're saving a password, you might consider encrypting it, or instead of storing the password, store the hash for the password.
First encrypt Username and Password strings with MD5, SHA-1 or other algorithm.
After use:
NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:encryptedUsername forKey:#"userName"];
[prefs setObject:encryptedPassword forKey:#"password"];
[prefs synchronize];
// To retrieve info
encryptedUsername = [prefs objectForKey:#"userName"];
encryptedPassword = [prefs objectForKey:#"password"];
After encrypt the user input data and compare the encrypted strings.

How to remove all NSUser Defaults except two objects?

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.

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.

How to write data to an xml file

I want to store data (preferences) into an xml file , but I can't achieve it. I took a look at appledeveloper web but it seems not clear to me .
I want to store the xml file on disk . My preferences that I created are some checkboxes (ih the user cheks the login box for example and I add this preference login to the xml file) , radio group, textfields
can you give me a good tutorial or an example that shows that please?
You usually store preferences using the NSUserDefaults class.
Example:
[[NSUserDefaults standardUserDefaults] setObject:#"Option1" forKey:#"PreferenceName1"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"PreferenceName2"];
And then read like this:
NSString *pref1 = [[NSUserDefaults standardUserDefaults] objectForKey:#"PreferenceName1"];
BOOL pref2 = [[NSUserDefaults standardUserDefaults] boolForKey:#"PreferenceName2"];

NSUserDefaults. setValue works, Not setBool

I trying to store some settings in NSUserDefaults, but It seems that the app won't store the setBool values.
This works:
[[NSUserDefaults standardUserDefaults] setValue: #"hello" forKey: #"test"];
[[NSUserDefaults standardUserDefaults] synchronize];
When I terminate the app and restart it, the value have been saved. However, when I do this:
[[NSUserDefaults standardUserDefaults] setBool: YES forKey: #"test"];
[[NSUserDefaults standardUserDefaults] synchronize];
It won't save after I close the app and restart it.
Should I file a bug report, or is there something I'm missing here?
Thanks
Edit:
I figure what I did wrong. In AppDelegate, I wanted to check if the boolForKey was set, and it it wasn't I did this:
if (![defaults boolForKey: #"test123"])
[defaults setBool: YES forKey: #"test123"];
... however, when it comes to boolWithKey, the "!" just check if the bool is YES or NO, not if its nil.
How can you be sure its not working? I tried your code and it works for me. Are you sure you are reading the Boolean in the correct way AFTER you write it?
This code SHOULD work:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:#"test"];
[defaults synchronize];
BOOL myBool = [defaults boolForKey:#"test"];
I had the same problem by having the following code in my AppDelegate to keep track of whether the user had seen a particular viewController to display a walkthrough, and then setting this Bool to NO after the user had seen it.
if (![standardUserDefaults boolForKey:#"firstViewOfVC"]) {
[standardUserDefaults setBool:YES forKey:#"firstViewOfVC"];
}
But then when you set it to NO later on and check if it "exists", you are actually seeing the NO boolean value and setting it back to yes. The quick fix is just to store the boolean value in an NSNumber object so that you can check for it's existence, independent of its value being YES or NO. See below:
if (![standardUserDefaults objectForKey:#"firstViewOfVC"]){
[standardUserDefaults setValue:[NSNumber numberWithBool:YES] forKey:#"firstViewOfVC"];
}
I had the exact same problem. Everything EXCEPT BOOLs were persisting correctly; but i was using some old coding styles from ios 3. recoded this way, everything works.
If anybody else is using old books.... here is an example
Bad stuff:
//////////// set / get bL2R
if (![[NSUserDefaults standardUserDefaults]
boolForKey:kL2RomanizationChoice]) {
[[NSUserDefaults standardUserDefaults]
setBool:YES
forKey:kL2RomanizationChoice];
bL2R = YES;
NSLog(#"L2Rom not found, set to YES.");
}
else {
bL2R = [[NSUserDefaults standardUserDefaults]
boolForKey:kL2RomanizationChoice];
NSLog(#"L2Rom found.");
if (bL2R) {
NSLog(#"L2Rom found to be YES.");
}
}
Good stuff:
if (![defaults boolForKey:kL2RomanizationChoice])
[defaults setBool:YES forKey:kL1RomanizationChoice];
L2String_Setting = [defaults objectForKey:kSecondLangChoice];
bL2R = [defaults boolForKey:kL2RomanizationChoice];
Update: sadly this only seemed to work briefly, and now is failing again... using Xcode 4.5.2. may just swap out bools for integers...
XCode 4.6 seems to have the same problem highlighted by hangzhouharry. A useful call is [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] to see if your key values are looking the way they should.
For example -> autoLogout = 0;
which was set as a Bool, [settings boolForKey:#"autoLogout"] returns nothing
[settings integerForKey:#"autoLogout"] returns 0 (as, sort of, expected)