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:

  1. In Xcode, choose File New File.

  2. Make sure the iOS category is selected on the left.

  3. Choose the Resources subcategory.

  4. Choose Settings Bundle as the file type and click Next.

  5. Set the filename as Settings.bundle.

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

Our Settings.bundle displayed in the Settings app on iOS Simulator

Figure 14-6. Our Settings.bundle displayed in the Settings app on iOS Simulator ...

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.