I have the following code aiming to catch the event of a NSUserDefaults value changing for a particular key.
[[NSUserDefaults standardUserDefaults] addObserver:self
forKeyPath:SOME_NSSTRING_VARIABLE
options:NSKeyValueObservingOptionNew
context:NULL];
- (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{NSLog (@"Changed for key %@", keyPath); }
But the observeValueForKeyPath is never getting called. I even tried replacing the SOME_NSSTRING_VARIABLE too with a string as mentioned in Observing value changes to an NSUserDefaults key but it has not helped.
Update: I am changing the NSUserDefaults from a tabview. The above code to monitor changes is in a different tab of the same tabviewcontroller. In the tab where I monitor for the changes (the tab where above code exists), if I add a :
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// NSLog(@"viewillappear");
NSUserDefaults *FUDefaults = [NSUserDefaults standardUserDefaults];
NSLog(@"The username obtained is: %@", [FUDefaults objectForKey:SOME_NSSTRING_VARIABLE]);
}
the updated NSUserDefaults value is obtained correctly, but the observeValueForKeyPath was never called.