Save user settings in iPhone/iPad app

309 views Asked by At

In my app I let users to pick a colour of background, tint and text. How can I save this settings after the app is closed? I mean when user again starts the app he wants to see the background, tint and text colour from previous usage.

This is how I have set the colours.

self.BackgroundView.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:242.0/255.0 blue:229.0/255.0 alpha:1.0];

[[[self navigationController] navigationBar] setTintColor:[UIColor colorWithRed:103.0/255.0 green:10.0/255.0 blue:10.0/255.0 alpha:1.0]];

self.QuoteText.textColor = [UIColor colorWithRed:131.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0];

Trying to solve by following Greg's (probably the right) answer gives SIGABRT error by clicking on following button:

- (IBAction)setcolor:(id)sender {

    UIColor *backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:220.0/255.0 alpha:1.0];
    self.BackgroundView.backgroundColor = backgroundColor;

    UIColor *tintColor = [UIColor colorWithRed:129.0/255.0 green:165.0/255.0 blue:148.0/255.0 alpha:1.0];
    [[[self navigationController] navigationBar] setTintColor:tintColor];

    UIColor *textColor = [UIColor colorWithRed:0.0/255.0 green:98.0/255.0 blue:139.0/255.0 alpha:1.0];
    self.QuoteText.textColor = textColor;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:backgroundColor forKey:@"BACKGCOLOR"];
    [defaults setObject:tintColor forKey:@"TINTCOLOR"];
    [defaults setObject:textColor forKey:@"TEXTCOLOR"];
    [defaults synchronize];

}
3

There are 3 answers

0
Greg On BEST ANSWER

If user pick up colour instead of

self.BackgroundView.backgroundColor = [UIColor colorWithRed:248.0/255.0 green:242.0/255.0 blue:229.0/255.0 alpha:1.0];

You can do

    UIColor *backgroundColor = [UIColor colorWithRed:248.0/255.0 green:242.0/255.0 blue:229.0/255.0 alpha:1.0];
    self.BackgroundView.backgroundColor = backgroundColor;
    NSData *backgroundColorData = [NSKeyedArchiver archivedDataWithRootObject:backgroundColor];

    UIColor *tintColor = [UIColor colorWithRed:103.0/255.0 green:10.0/255.0 blue:10.0/255.0 alpha:1.0];
    [[[self navigationController] navigationBar] setTintColor:tintColor];
    NSData *tintColorData = [NSKeyedArchiver archivedDataWithRootObject: tintColor];

    UIColor *textColor = [UIColor colorWithRed:131.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0];
    self.QuoteText.textColor = textColor;
    NSData *textColorData = [NSKeyedArchiver archivedDataWithRootObject: textColor];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject: backgroundColorData forKey:@"BACKGCOLOR"];
    [defaults setObject:tintColorData forKey:@"TINTCOLOR"];
    [defaults setObject:textColorData forKey:@"TEXTCOLOR"];
    [defaults synchronize];

You do the same for tint and text colour (just use different key).

In the view controller where you want to change your view colour in viewDidLoad you do:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *backgColorData = [defaults objectForKey:@"BACKGCOLOR"];
UIColor *backgColor = [NSKeyedUnarchiver unarchiveObjectWithData:backgColorData];
NSData *tintColorData = [defaults objectForKey:@"TINTCOLOR"];
UIColor *tintColor = [NSKeyedUnarchiver unarchiveObjectWithData:tintColorData];
NSData *textColorData = [defaults objectForKey:@"TEXTCOLOR"];
UIColor *textColor = [NSKeyedUnarchiver unarchiveObjectWithData: textColorData];
if (backgColor)
    self.BackgroundView.backgroundColor = backgColor;
if (tintColor)
    [[[self navigationController] navigationBar] setTintColor:tintColor];
if (textColor)
    self.QuoteText.textColor = textColor;
0
Flavien Volken On

Apple is providing the NSUserDefaults for this, it does act like the preferences for your user / application. Example taken from here

Saving :

NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults]
    setObject:valueToSave forKey:@"preferenceName"];

To get it back later :

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
    stringForKey:@"preferenceName"];
0
Vaionixx On

Test using nsuserdefaults to save the settings or, write to a plist.