14.9. Responding to Changes in App Settings

Problem

Your application exposes a settings bundle to the user. You want to get notified of the
changes the user has made to your application’s settings (while the
application was in the background) as soon as your application is
brought to the foreground.

Solution

Register for the NSUserDefaultsDidChangeNotification
notification.

Discussion

Applications written for iOS can expose a bundle file for their
settings. These settings will be available to users through the Settings
application on their device. To get a better understanding of how this
works, let’s create a settings bundle:

In Xcode, choose File → New File.

Make sure the iOS category is selected on the left.

Choose the Resources subcategory.

Choose Settings Bundle as the file type and click Next.

Set the filename as Settings.bundle.

Click Save.

Now you have a file in Xcode named Settings.bundle. Leave this file as it is,
without modifying it. Put the code in this recipe’s Solution into your root view controller
and run the application. Press the Home button on the device and go to
the device’s Settings application. If you have named your application
“foo” you will see “Foo” in the Settings application, as shown in Figure 14-6
(the name of the sample application I created is “Responding to Changes
in App Settings”).