user818700
user818700

Reputation:

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!

Upvotes: 0

Views: 54

Answers (2)

Javi Campaña
Javi Campaña

Reputation: 1481

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.

Upvotes: 1

Merlevede
Merlevede

Reputation: 8170

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.

Upvotes: 1

Related Questions