I'm using NSUserDefaults in my app and I would like to be notified when a particular value is changed. For that, I added the following lines in viewDidLoad:
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
[settings synchronize];
[settings addObserver:self forKeyPath:@"pref_server" options:NSKeyValueObservingOptionNew context:NULL];
And the method to be notified:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"Change");
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
if (object == settings && [keyPath isEqualToString:@"pref_server"])
{
NSLog(@"Server did change");
}
}
Unfortunately, the latter is never called...@"pref_server" is the item identifier I have set in Root.plist, in Settings.bundle. What am I doing wrong?