O'Reilly logo

iOS 6 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required