Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load/save settings in iOS

I have the following two procedures defined in my AppDelegate. saveSettings and loadSettings. I am calling my loadSettings procedure in the AppDidFinishLaunching method, and I am calling the saveSettings procedure in my settings view, once the save button is clicked.

Both methods seem to be called at the right time, the right number of times (once), and using the correct data. my settings object gets the right data, but the data does not seem to be actually saving. When I run the load code, my resulting variables are coming back empty (not nil).

I tried putting the same loading code in a different view and it works fine, but for some reason, I am not getting results in my appDelegate.

- (void)applicationDidFinishLaunching:(UIApplication *)application {         [window addSubview:navigationController.view];     [window makeKeyAndVisible];     [self loadSettings];     [self setDefaults]; }  -(void)loadSettings{     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];     settings.masterLocation = [prefs objectForKey:@"masterLocation"];     settings.masterPort = [prefs objectForKey:@"masterPort"];     settings.userName = [prefs objectForKey:@"userName"];     settings.passWord = [prefs objectForKey:@"passWord"];     settings.autoLogin=[prefs objectForKey:@"autoLogin"];      if (settings.autoLogin == nil)         settings.autoLogin=@"N"; }  -(void)saveSettings:(SharedData *)d{     settings=d;      NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];     [prefs setObject:settings.masterLocation forKey:@"masterLocation"];     [prefs setObject:settings.masterPort forKey:@"masterPort"];     [prefs setObject:settings.userName forKey:@"userName"];     [prefs setObject:settings.passWord forKey:@"passWord"];     [prefs setObject:settings.autoLogin forKey:@"autoLogin"];    } 
like image 257
Dutchie432 Avatar asked Apr 23 '09 16:04

Dutchie432


People also ask

Where are UserDefaults stored?

Storing Data in User Defaults The user's defaults database is stored on disk as a property list or plist. A property list or plist is an XML file. At runtime, the UserDefaults class keeps the contents of the property list in memory to improve performance.

How do I save UserDefaults in Swift?

Updating the Prospects initializer so that it loads its data from UserDefaults where possible. Adding a save() method to the same class, writing the current data to UserDefaults . Calling save() when adding a prospect or toggling its isContacted property.

What is UserDefaults in Swift?

An interface to the user's defaults database, where you store key-value pairs persistently across launches of your app.

How much data can you store in UserDefaults?

Currently, there is only a size limit for data stored to local user defaults on tvOS, which posts a warning notification when user defaults storage reaches 512kB in size, and terminates apps when user defaults storage reaches 1MB in size. For ubiquitous defaults, the limit depends on the logged in iCloud user.


1 Answers

Doh.

In saveSettings, I was missing my [prefs synchronize];

like image 200
Dutchie432 Avatar answered Oct 01 '22 13:10

Dutchie432