Add Settings to iOS app

Tram Ho

In iOS, the Foundation framework provides a low-level mechanism for storing Preference data. There are two ways to change these preferences data:

  1. Change right inside the app
  2. Use the Settings bundle to manage preferences from the Settings app

According to Apple docs: An app can display settings both in the app and in the phone’s Settings app. It depends on how you want the user to interact with the settings.

The table below contains a list of the Control Types you can use in settings.bundle

Every app with an Install package has at least one option page, called the main page. If your app has only a handful of options, the main page might be the only page you need. However, if the number of options is too large to fit the main page, you can create subpages that link to the main page or other subpages. There is no specific limit to the number of subpages you can create, but you should try to keep your preferences as simple and easy to navigate as possible.

A Settings bundle is named Settings.bundle and is located in the top-level directory of the app bundle. This bundle contains one or more Settings page files describing individual option pages. It may also include other supporting files needed to render your preferences, such as localized string or image.

You can localize the Info.plist file and show the settings in your desired language.Please read more in the official docs.

Add a Settings Bundle

To add a settings bundle to the Xcode project:

  1. Choose File> New> New File.
  2. Select Resource in the iOS section, and select Settings Bundle template.
  3. Name the Settings.bundle

In addition to adding a new Settings bundle to your project, Xcode automatically adds that bundle to the Copy Bundle Resources build phase of the app target. Therefore, all you have to do is modify the property list files of the Settings bundle and add any necessary resources.

The new settings bundle has the following structure:

Click the Root.plist file. You will see a list of properties as follows:

Preference items is an array of dictionary controls. Go to Figure 1 for all the Controls we can add in the settings app bundle. For this demo app, I only need one group, one to show the app version number, one to show the build number, and a toggle switch to reset the app. So delete all the items in the array preference items and add the items mentioned above.

So for our application, the Root.plist file will look like this:

The identifier is the Userdefaults key that you can use inside the code to make the appropriate changes. Don’t forget to add default values ​​for Title. If not, it won’t appear in settings.

Tip: If you are worried about the order of the controls in the plist, Right click on the plist file and open it as source code. Can be easily edited xml instead of plist directly.

The setup UI will look like this for the plist file above:

Now, let’s create a class called SettingsBundleHelper.swift to handle the setting changes …

I added the code to reset the data and set the version number in AppDelegate like this:

Update Defaults with Observers

The above method has a limitation: we have to call the code every time the app starts. Also, you can set an observer for UserDefaults by using UserDefaults.didChangeNotification.

But if you are using multiple UserDefaults, then you should add the proper condition check and execute the above code. Also in versions of iOS prior to iOS 9, it is possible that memory issues can occur when you constantly receive multiple notifications like this. Before iOS9, we needed to delete observer to manage memory well.

Note: This is a basic guide to settings.bundle. There are some more advanced options like the sub settings page, adding a slider, etc. that are very easy to do that I haven’t covered here.

Note: If the setting doesn’t show you, try using the App Switching UI (double tap the Home button) to turn off the Installed Apps and launch it again.

Full Project Github link (code is in swift 3).
Reference source: Adding settings to your iOS app

Share the news now

Source : Viblo