In iPhone sdk when to store some value to NSUserDefaults then that values are visible from “/Library/Preferences/.plist” .So this is not safe because every one can read what ever you have store to NSUserDefaults. It is very important when you want to store username and password or some secure information.
To provide security to you NSUserDefaults value i have some trick for you.
You can also store UIColor to NSUserDefaults by using NSKeyedUnarchiver.


//Store to USUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *txtData = [NSKeyedArchiver archivedDataWithRootObject:@"simpleText"];
NSData *textFeildData = [NSKeyedArchiver archivedDataWithRootObject:txtPassword.text];
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:txtPassword.textColor];
[defaults setObject:colorData forKey:@"textColor"];
[defaults setObject:txtData forKey:@"simpleText"];
[defaults setObject:textFeildData forKey:@"textFeildData"];

[defaults synchronize];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *txtData = [defaults objectForKey:@"simpleText"];
NSData *textFeildData = [defaults objectForKey:@"textFeildData"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
NSString *strSimpleText = [NSKeyedUnarchiver unarchiveObjectWithData:txtData];
NSString *strPassword = [NSKeyedUnarchiver unarchiveObjectWithData:textFeildData];

NSLog(@"%@\n%@",strSimpleText,strPassword);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s