How to determine when Settings change on iOS
The syntax is for Swift 2. Using Swift you would do something like this to subscribe to changes for the NSUserDefaults :
NSNotificationCenter.defaultCenter().addObserver(self, selector: "defaultsChanged:", name: NSUserDefaultsDidChangeNotification, object: nil)
Then create the method like this :
func defaultsChanged(notification:NSNotification){
if let defaults = notification.object as? NSUserDefaults {
//get the value for key here
}
}
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).