23

After a lot of searching I was not able to find whether you need to pass a dictionary object to:

[NSUserDefaultsDidChangeNotification addObserver: forKeyPath: options: context:];

and what should be provided in options if I want to be notified for even a single change in the userDefaults. Also what is keypath?

halfer
  • 19,824
  • 17
  • 99
  • 186
neha
  • 6,327
  • 12
  • 46
  • 78

1 Answers1

66

NSUserDefaultsDidChangeNotification is just a notification that is sent out when the defaults are changed. To listen out for it you need this code :

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(defaultsChanged:)  
                   name:NSUserDefaultsDidChangeNotification
                 object:nil];

This will call the method defaultsChanged: when the notification is fired. You need to implement this method like this :

- (void)defaultsChanged:(NSNotification *)notification {
    // Get the user defaults
    NSUserDefaults *defaults = (NSUserDefaults *)[notification object];

    // Do something with it
    NSLog(@"%@", [defaults objectForKey:@"nameOfThingIAmInterestedIn"]);
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • 13
    It is important to remove the observer in the dealloc method. If a destroyed class is still registered as an observer, the app will crash upon notification. Use: [[NSNotificationCenter defaultCenter] removeObserver:self]; – maralbjo Jan 18 '11 at 11:29
  • This throws an error. Someone might be staring at it for 15 minutes wondering what they did wrong to find out the "notification" is spelled wrong. Just an FYI – RyeMAC3 Apr 23 '11 at 03:32
  • 1
    @deanWombourne: in defaultsChanged, where you say "// Do stuff in here!" How do you get at the information in the notification? It doesn't have userInfo, says the class reference. – Ziggy Jul 28 '11 at 19:50
  • userInfo is anything that the initial sender of the notification wanted to give to you - in this case it's `nil`. The other bit of information you get is `notification.object` which is the sender of the notification. See my edited answer for an example ;) – deanWombourne Jul 28 '11 at 19:55
  • 10
    @deanWombourne Is there a way to know which user default has changed? –  Oct 11 '11 at 16:39
  • will this be called for any NSUserdefaults container? or just for standarduserdefaults? – Sebastian Flückiger Apr 17 '15 at 11:32
  • @SebastianFlückiger The example above will listen to all user defaults because I passed in `nil` as the last argument to `addObserver:selector:name:object:` which means I don't care who is firing the notification. If I only wanted to listen to a specific user defaults instance when I would have to pass it into that method as the `object`. `NSNotification` also has an `object` property so you can check where a notification came from when you recieve it. – deanWombourne Apr 17 '15 at 13:18
  • @deanWombourne i just found out that extensions do not trigger the group notification. going to try MMWormhole for it. – Sebastian Flückiger Apr 17 '15 at 13:29
  • @SebastianFlückiger They might only trigger notifications within the extension - you probably won't get a corresponding notification in the main app. sigh. I've never heard of MMWormHole but it looks interesting. Good luck! – deanWombourne Apr 17 '15 at 14:58