12.9. Responding to Changes in App Settings by Vandad Nahavandipoor

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

12.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 FileNew 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 12-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 12-6. Our Settings.bundle displayed in the Settings app on iOS Simulator ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required