Reputation: 5661
My app has a setting that allows me to turn off ui sounds, or turn it on. I use this for the value:
+ (void)setUISoundsEnabled:(BOOL)UISoundsEnabled
{
__UISoundsEnabled = UISoundsEnabled;
}
here is a notification in my view controller that listens for preference changes and then updates the sound settings.
- (void)preferencesDidChange:(NSNotification *)note
{
NSMutableArray *changedPreferences = note.object;
if ([changedPreferences containsObject:@"localPlayUISounds"]) {
[FHSSound setUISoundsEnabled:PREFS.localPlayUISounds];
}
2 questions:
First question: How would go about getting the settings saved in PREFS to match the settings saved in the BOOL at the top.
Second question: How would I implement NSUserDefaults to save and load this data. To be specific where exactly am I implementing NSUserDefaults to save, and load this data. I'm not familiar with NSUserDefaults so examples would be very helpful
Please let me know if you need any more code or have any more questions
Upvotes: 0
Views: 358
Reputation: 5661
Here is how I solved it without using NSUserDefaults.
- (void)viewDidLoad
{
[super viewDidLoad];
[self setSoundPreferences];
}
The notification is set to listen for the change in prefs
- (void)preferencesDidChange:(NSNotification *)note
{
NSMutableArray *changedPreferences = note.object;
if ([changedPreferences containsObject:@"localPlayUISounds"]) {
[FHSSound setUISoundsEnabled:PREFS.localPlayUISounds];
}
else if ([changedPreferences containsObject:@"localPlayAlertSounds"]) {
[FHSSound setAlertSoundsEnabled:PREFS.localPlayAlertSounds];
}
and lastly setting the preferences at launch
#pragma mark (launch)
- (void)setSoundPreferences
{
[FHSSound setUISoundsEnabled:PREFS.localPlayUISounds];
[FHSSound setAlertSoundsEnabled:PREFS.localPlayAlertSounds];
}
Upvotes: 0
Reputation: 18826
It's very simple. You use NSUserDefaults
as-is; there's no need to subclass.
// read the setting when you start the app
flag = [[NSUserDefaults standardUserDefaults] boolForKey:"someKey"]
// set the setting if user can change it inside your app
[[NSUserDefaults standardUserDefaults] setBool:flag forKey:"someKey"]
It's probably best to create a settings plist file so that these settings can be then changed in the Settings app. If you do that, you should also listen to NSUserDefaultsDidChangeNotification
in case user changes the settings in the Settings app while yours is in the background.
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:)
name:NSUserDefaultsDidChangeNotification object:nil];
...
- (void)defaultsChanged:(id)sender {
[[NSUserDefaults standardUserDefaults] synchronize];
// and notify your observers as necessary based on new settings...
}
Apps with advanced settings also often contain a pre-populated plist with "initial" default values, because your standardUserDefaults
will contain no values until the user enters something in Settings app:
NSURL *defaultSettingsURL = [[NSBundle mainBundle] URLForResource:@"DefaultSettings" withExtension:@"plist"];
self.bundledDefaults = [NSDictionary dictionaryWithContentsOfURL:defaultSettingsURL];
You can dump bundledDefaults
into settings on first run or simply use them as a back-up any time you read from the user defaults.
Upvotes: 1