Chapters

Hide chapters

Catalyst by Tutorials

Third Edition · iOS 15 · Swift 5.6 · Xcode 13.3

Section I: Making a Great iPad App

Section 1: 7 chapters
Show chapters Hide chapters

7. Preferences & Settings Bundle
Written by Andy Pereira

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

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 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. Tap the Home button on the simulator, and open Settings. Right now, you’ll see only the default settings.

Settings in simulator.
Settings in simulator.

You’ll 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.

Adding Settings bundle to your app.
Exmiqq Nalqalyv pudnju su ziow ipb.

Default files created in the Settings bundle.
Vuwoepz ruwus jneepas ak ljo Yuznomwr zuvvva.

Default settings of your app.
Budoafc pomhelfr ax xiov ocs.

Adding Preference Items.
Ukzefg Pyopedaqta Uyutr.

New items created in Settings.
Nad uzizd fliesaq ik Kisqeckh.

Responding to Change

To see any preference changes in your app, you need to 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
  }
}
Environment Overrides.
Ejmomadsocb Aduhmaqoc.

Entry with light background.
Acqfy keqc pilyf niwvdluucq.

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
  )
}
Includes signature when sharing an entry.
Ihshidah wihzazepi nluj vfihosw az enwyv.

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.

Sharing.plist file created.
Mzubelr.xxext fequ tweakid.

All items in Sharing.plist.
Udt ofapm od Wqonotk.hyovp.

All items in Root.plist
Ijp irucc ik Neev.mnarn

New group created in Settings.
Fix rdiib jniojuj ab Xuhvamcc.

Sharing group in Settings.
Braxuqr mkiar ig Bawlofcc.

Key Points

  • Settings and preferences are helpful for your users. For example: Adding your signature when sharing or using a light background for text views.
  • 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.

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 reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now