I'm developing an iOS application with latest SDK.
I want to know when a property on NSUserDefaults
changes it value.
I have found this, but it is specific for macOS:
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:[@"values." stringByAppendingString: @"MyPreference"] options:NSKeyValueObservingOptionNew context:NULL];
How can I do this on iOS?
try out the NSUserDefaultsDidChangeNotification
with this code snippet:
- (id)init { self = [super init]; if(self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:) name:NSUserDefaultsDidChangeNotification object:nil]; } return self; } - (void)defaultsChanged:(NSNotification *)notification { // Get the user defaults NSUserDefaults *defaults = (NSUserDefaults *)[notification object]; NSLog(@"%@", [defaults objectForKey:@"yourIntrestedObject"]); } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
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