Book Image

Mastering iOS 14 Programming - Fourth Edition

By : Mario Eguiluz Alebicto, Chris Barker, Donny Wals
Book Image

Mastering iOS 14 Programming - Fourth Edition

By: Mario Eguiluz Alebicto, Chris Barker, Donny Wals

Overview of this book

Mastering iOS 14 development isn’t a straightforward task, but this book can help you do just that. With the help of Swift 5.3, you’ll not only learn how to program for iOS 14 but also be able to write efficient, readable, and maintainable Swift code that reflects industry best practices. This updated fourth edition of the iOS 14 book will help you to build apps and get to grips with real-world app development flow. You’ll find detailed background information and practical examples that will help you get hands-on with using iOS 14's new features. The book also contains examples that highlight the language changes in Swift 5.3. As you advance through the chapters, you'll see how to apply Dark Mode to your app, understand lists and tables, and use animations effectively. You’ll then create your code using generics, protocols, and extensions and focus on using Core Data, before progressing to perform network calls and update your storage and UI with the help of sample projects. Toward the end, you'll make your apps smarter using machine learning, streamline the flow of your code with the Combine framework, and amaze users by using Vision framework and ARKit 4.0 features. By the end of this iOS development book, you’ll be able to build apps that harness advanced techniques and make the best use of iOS 14’s features.
Table of Contents (22 chapters)

Further exploring Dark Mode

In the previous sections, we gave you a lot to think about when either creating or migrating existing apps to Dark Mode under specific circumstances. In this section, we'll take a look at a couple of little "nice to knows" that should always be in the back of your mind when approaching Dark Mode.

Using Dark Mode with SwiftUI

With the announcement of SwiftUI back in June 2019, a massive shift in focus on UI-based development took place. Released at the same time as Dark Mode, and as expected, SwiftUI takes full advantage of switching appearances.

Let's start by taking a look at how we could detect dark mode programmatically in SwiftUI:

  1. First, we'll create an environment variable that allows us to access the current state of the appearance of the device:
    @Environment(\.colorScheme) var appearance
  2. Next, let's use a simple ternary operator to display some text based on the current appearance:
    Text(appearance == .dark ? "Dark Appearance" : "Light Appearance")

It really is that simple.

Now, let's have a look at the options available to us in the automatic preview window. SwiftUI uses PreviewProvider, which allows us to display dynamically what we are designing/developing.

To enable Dark Mode in PreviewProvider, simply add the following highlighted code and hot refresh:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environment(\.colorScheme, .dark)
    }
}

Here, we've added a modifier to set the .colorScheme environment variable to .dark. If we want to preview both .light and .dark side by side, we can simply do the following:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView().environment(\.colorScheme, .light)
             ContentView().environment(\.colorScheme, .dark)
        }
    }
}

Tip

To learn more about SwiftUI, take a look at Learn SwiftUI, available from Packt Publishing: https://www.packtpub.com/business-other/learn-swiftui.

Programatically handling changes with trait collection

During the development of your new app, there could be a couple of occasions where you might need to handle a specific scenario based on the current appearance. However, we'll need to take a slightly different approach to this than what we did with the SwiftUI example previously.

The interface style is part of the UITraitCollection class (which, in turn, is part of UIKit). We can do a conditional check against a value using the following anywhere in our ViewController:

traitCollection.userInterfaceStyle == .dark

Unlike SwiftUI, we can't just use a simple ternary operator as there are more than two values for userInterfaceStyle:

public enum UIUserInterfaceStyle : Int {
    case unspecified
    case light
    case dark
}

Unspecified is an option too (think Any, back in our asset catalog), so it's best to use another approach when detecting changes to our interface style.

Let's start by heading back into our ViewController.swift file and adding in the following override function:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    // Logic here    
}

This override is called whenever a change is made to a trait (such as appearance). From this, we now action any changes we would like to make, but the problem we have is traits are used for more than just appearances, and this override could be called for a variety of reasons.

So, if we are looking particularly for changes in our appearance, we can use the previousTrait property passed into our delegate function and compare against the current system trait – if there is a difference, we know the appearance has changed. Let's take a look at how we'd do this:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    let interfaceAppearanceChanged = previousTraitCollection?.hasDifferentColorAppearance(comparedTo: traitCollection)
}

By using the hasDifferentColorAppearance method, we can now easily compare the previous trains against the current one to see whether there have been any changes – the resulting method returns a Boolean, so we can use this at our convenience.

Specifying an appearance for views, ViewControllers, and windows

You may, in some circumstances, wish to specify an appearance based on a particular area of your app or if you are migrating to dark mode (but need a little more time for a certain feature). Simply drop in the following appropriate code to meet your desire.

Views

Here, we'll create and instantiate a basic UIView:

let view = UIView()
view.overrideUserInterfaceStyle = .dark // .light

We assign either a light or dark value.

ViewController

If we wanted to do this in a UIViewController, we would simply just do the following:

overrideUserInterfaceStyle = .dark

Again, we assign either a light or dark value (usually within viewDidLoad()).

Window

If we need to access the current window, we could do so as follows:

for window in UIApplication.shared.windows {
    window.overrideUserInterfaceStyle = .dark
}

(This is not a recommended approach and you would be hard-pressed to find any real reason to want to do this…)

Accessibility in Dark Mode

Ask around and someone will joke about how Dark Mode has existed in iOS for years, either as the Classic Invert or Smart Invert accessibility feature. I even had it on one of my slides at a conference about 2 months prior to Dark Mode being officially announced.

But with this in mind, a lot of things started to be said about accessibility in iOS – some comments referring to Dark Mode as "Apple finally supporting accessibility," which I have to be honest makes me very sad.

Accessibility has always played a massive part in iOS regardless of the appearance – but, even with the introduction of Dark Mode, this still goes unchanged as Dark Mode supports all accessibility features.

If you refer back to an earlier section in this chapter, Core development concepts in Dark Mode, you'll remember that we mentioned the option to schedule our light and dark appearances – much like you could with Nightshift that was introduced in iOS 9, again another element with a focus on accessibility.

In this section, we went a little outside of the box with regard to Dark Mode and stepped away from the basic implementation, allowing us to look at the wider options available to use and things to think about when implementing Dark Mode in our apps.