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"]; }
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.
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.
An interface to the user's defaults database, where you store key-value pairs persistently across launches of your app.
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.
Doh.
In saveSettings, I was missing my [prefs synchronize];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With