Chapters

Hide chapters

Catalyst by Tutorials

First Edition · iOS 13 · Swift 5.1 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

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)
  let overrideColor = UIColor.white
  if overrideColorPreference {
    entryCell.contentView.backgroundColor = overrideColor
    textView.textColor = UIColor.black
  } else {
    entryCell.contentView.backgroundColor = nil
    textView.textColor = UIColor.label
  }
}

@IBAction private func share(_ sender: Any?) {
  guard var textToShare = textView.text,
    !textToShare.isEmpty else { return }
  if let namePreference = UserDefaults
    .standard.string(forKey: namePreference),
    UserDefaults
      .standard.bool(forKey: signaturePreference) {
    textToShare += "\n -\(namePreference)"
  }
  let activityController
    = UIActivityViewController(activityItems: [textToShare],
                               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.

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