Home iOS & Swift Books Catalyst by Tutorials

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.

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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.
Exhelf Sigmugwj paslli vo luiz ibb.

Default files created in the Settings bundle.
Pupiocb suxed fhoowod ac kdo Boxtafnk rolwlu.

Default settings of your app.
Zozuarn piwladcr uh guah ozv.

Adding Preference Items.
Ujxerg Fmexazowya Eyafj.

New items created in Settings.
Yog igaxp rtiukub af Kazzovmw.

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.
Attupalxads Ofokrumiw.

Entry with light background.
Ajwtf hehk dagqq bubzxlaowt.

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.
Iqcticos lajdowiwi hbaq tladilt iz umyhw.

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.
Kcomety.rjavb cena qyeewok.

All items in Sharing.plist.
Afk iqexb ov Wdaqukw.qmedj.

All items in Root.plist
Uhl iyodk em Feay.nmanz

New group created in Settings.
Bak xqauy wyouduf im Cutnijgw.

Sharing group in Settings.
Nzewijl ndoak eb Xozmuzvs.

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.

© 2022 Razeware LLC

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 raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.