Posts Tagged ‘NSKeyedArchiver’

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