2

I use some settings from a settings.bundle in my iOS App. I would like to monitor changes from these settings. I already know I can do this by using NotificationCenter.default.addObserver(self, selector: #selector(onSettingsChanged), name: UserDefaults.didChangeNotification, object: nil).

However, this only informs me that a setting has changed but does not tell me which one? Is there a way to register an observer only for a specific setting, or to provide the changed setting as parameter to the observer?

ali
  • 1,475
  • 4
  • 22
  • 40
  • 1
    Not a duplicate of that question. This question asks how to either observe a specific setting, or how to know which setting changed. That question doesn't ask for these things, and that question's answers don't answer these things. – rob mayoff Jun 06 '18 at 08:14
  • @robmayoff Thanks for the support, but it actually is a duplicate in some way. If you read the discussion in the comments of the other question, you will see that there is an answer, stating that what I would like to do is not possible out of the box. You have to save the last state of the settings and check every setting for changes manually. :( – ali Jun 06 '18 at 08:55
  • 1
    You can observe a specific setting using KVO. – rob mayoff Jun 06 '18 at 15:16

1 Answers1

1

You can pass an object that contains the changed settings.

for eg.

var settings = ["setting1": "changes"]

as user change setting you need to update settings object

settings["setting1"] = "something changed"

and you can pass settings object in notification.

// Post a notification to inform about settings
NotificationCenter.default.post(name: NSNotification.Name(rawValue: UserDefaults.didChangeNotification), object: nil, userInfo: settings) 
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • But where is the connection between my Settings.bundle file and the variable `settings`? – ali Jun 06 '18 at 07:39