views:

38

answers:

2

I have created a custom settings using the standard root.plist approach for the iphone. I'm wondering if there's a way to determine when the user changes those settings in my app...

+1  A: 

Register to receive NSUserDefaultsDidChangeNotification notifications. It's not obvious, but the iOS Application Programming Guide describes it as such:

Preferences that your application exposes through the Settings application are changed

Robot K
+2  A: 

You can listen for NSUSerDefaultsDidChange-notifications with this:

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

Whenever the NSUserDefaults changes, defaultsChanged will be called.

Don't forget to call [[NSNotificationCenter defaultCenter] removeObserver:self]; when you want to stop listening for these notifications (you should also do this when object gets deallocated).

Emil
ah ok, make sense. now my next question is, how can I determine which settings were changed?
Ben
+1. Typically you'd addObserver in `-init` (or `-application:didFinishLaunchingWithOptions:` for the app delegate) and removeObserver in `-dealloc`. This is easier than keeping track of how many times you've registered (if you addObserver twice, you get called twice each time the notification is posted, IIRC).
tc.
@tc. Yep, good idea.
Emil
@Ben There is no real way to determine which settings were changed, but if you look for something specific, try storing the old version when the notifications gets run, then check against that the next time.
Emil
now I'm running into another problem. here's my flow:1) log into app (causes - settings to be loaded, and copied (to compare later for changes)2) exit app, go into settings, change setting3) go back into my app, check settings and compare w/ old ones, but the new settings aren't noticed, even after calling 'synchronize' on the standardUserSettings object.
Ben
Ben