Ben
Ben

Reputation: 16534

How to determine when Settings change on iOS

I have created a custom Settings.app bundle 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...

Upvotes: 33

Views: 25581

Answers (7)

Kris Markel
Kris Markel

Reputation: 12112

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

Upvotes: 8

Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40247

SWIFT 4

Register observer in viewController,

NotificationCenter.default.addObserver(self, selector: #selector(settingChanged(notification:)), name: UserDefaults.didChangeNotification, object: nil)

Selector implementation

 @objc func settingChanged(notification: NSNotification) {
    if let defaults = notification.object as? UserDefaults {
        if defaults.bool(forKey: "enabled_preference") {
            print("enabled_preference set to ON")
        }
        else {
            print("enabled_preference set to OFF")
        }
    }
}

Upvotes: 8

Joseph
Joseph

Reputation: 6032

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
    }
}

Upvotes: 14

Keerthesh
Keerthesh

Reputation: 77

In iOS10, try this:

UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    // Your code here
}

Upvotes: -3

iosCurator
iosCurator

Reputation: 4466

Listen to change in settings

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

Remember to remove the observer, once this view controller is no longer in memory.

Upvotes: 1

An example accessing an app specific Bool type setting with key "instantWeb":

func observeUserDefaults(notification: NSNotification) {
    print("Settings changed")
    if let defaults = notification.object as? NSUserDefaults {
        if defaults.valueForKey("instantWeb") as! Bool==true {
            print("Instant Web ON")
        }
    }
}

Upvotes: 2

Emil
Emil

Reputation: 7256

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).

Upvotes: 46

Related Questions