Chapters

Hide chapters

Catalyst by Tutorials

Second Edition · iOS 14 · Swift 5.3 · Xcode 12.2

7. Preferences & Settings Bundle
Written by Andy Pereira

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Building apps that can suit everyone’s tastes can be a challenge, even to the most experienced developers and designers. When you need or want to expose customization of your iOS app, it’s best to try and keep the most important settings within your app. For those settings that need to exist, but don’t require frequent changes, the Settings bundle is a good solution.

The Human Interface Guidelines (https://apple.co/2HY5vtf) caution against putting frequently used settings within the iOS Settings app. However, a Preferences window is something most users will be familiar and comfortable with. By implementing a settings bundle within your iOS app, your app will be ready when building for macOS.

Getting started

To begin, open the starter project for this chapter. Select any iPad for the active scheme, then build and run. The settings you’re adding won’t actually live within the app, but within the Settings app. Press the Home button on the simulator, and open Settings. Right now, you’ll see only the default settings.

You’re going to add a few preferences to this app:

  • Currently, when the app runs with the system in dark mode, it will make the text view dark. Sometimes, users might prefer the area they type or read to be lighter. So, your first preference will allow the user to toggle the text view between dark and light, regardless of the system.
  • Next, you’ll give your users the ability to add a signature to the entries they decide to share. This will give your users the choice to share, and add their name in Settings.

Adding the settings bundle

To add your app and its settings to the system Settings app, in your project, select the Journalyst group, and then File ▸ New ▸ File…. Under the Resource header, select Settings Bundle.

Responding to change

You won’t be able to actually see any changes if you don’t listen for changes to user defaults. Open EntryTableViewController.swift, and add the following to the end of viewDidLoad():

UserDefaults.standard
.addObserver(self,
             forKeyPath: colorPreference,
             options: .new,
             context: nil)
override func observeValue(forKeyPath keyPath: String?,
                           of object: Any?,
                           change: [NSKeyValueChangeKey : Any]?,
                           context: UnsafeMutableRawPointer?) {
  if keyPath == colorPreference {
    updateEntryCellColor()
  }
}

private func updateEntryCellColor() {
  let overrideColorPreference = UserDefaults
    .standard.bool(forKey: colorPreference)
  if overrideColorPreference {
    entryCell.contentView.backgroundColor = .white
    textView.textColor = .black
  } else {
    entryCell.contentView.backgroundColor = nil
    textView.textColor = .label
  }
}

private var shareText: String? {
  guard var textToShare = textView.text,
        !textToShare.isEmpty else { return nil }
  if let namePreference
      = UserDefaults.standard.string(forKey: namePreference),
     UserDefaults.standard.bool(forKey: signaturePreference) {
    textToShare += "\n-\(namePreference)"
  }
  return textToShare
}
@IBAction private func share(_ sender: Any?) {
  guard let shareText = shareText else { return }
  let activityController
    = UIActivityViewController(activityItems: [shareText],
                               applicationActivities: nil)
  if let popoverController
      = activityController.popoverPresentationController {
    popoverController.barButtonItem
      = navigationItem.rightBarButtonItem
  }
  present(activityController,
          animated: true,
          completion: nil)
}

Child panes

Settings.app can do more than just show a simple list of items. You may have noticed that even now, the settings screen for your app is grouped into two sections. You can also add groups and even multiple pages. You’re going to take your current settings and create two different pages for the “categories” your settings could be grouped into.

Key Points

  • Settings and preferences are helpful for your users.
  • Keeping your app’s preferences in Settings.plist quickly provides a standard way for your users to find available customizations.
  • Using multiple plists bring hierarchy and organization to your preferences.

Where to go from here?

You can find out about all the other controls available to you in a settings bundle from Apple’s Preferences and Settings Programming Guide. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now