24
Accessibility

iOS is the most accessible mobile platform in the world. Whether a user needs support for vision, hearing, motor skills, or learning challenges, iOS provides ways to help. Most accessibility features are built into the system, so you, the developer, do not need to do anything. Some allow the developer to provide an even richer experience for the user, often with very little work on the developer’s part. Let’s take a look at some of the accessibility options that iOS provides.

VoiceOver

VoiceOver is an accessibility feature that helps users with visual impairments navigate your application’s interface. Apple provides hooks into the system that allow you to describe aspects of your interface to the user. Most UIKit views and controls automatically provide useful information to the user, but it is often beneficial to provide additional information that cannot be inferred. And you will always need to provide the information yourself for custom views or controls you create.

These hints to the user are largely provided through the UIAccessibility protocol. Let’s take a look at this protocol and the information that it provides.

The UIAccessibility protocol is an informal protocol that is implemented on all of the standard UIKit views and controls. An informal protocol is a looser contract than the formal protocols that you have been introduced to before. A formal protocol is declared using the protocol keyword and declares a list of methods and properties that must be implemented by something that conforms to that protocol. An informal protocol is implemented as an extension on NSObject; therefore, all subclasses of NSObject implicitly conform to the protocol.

You might be wondering why UIAccessibility is not a regular, formal protocol like the others you have seen throughout this book. Informal protocols are a legacy of the days when Objective-C did not have optional methods in formal protocols. Informal protocols were a workaround to solve this issue. Essentially, they required every NSObject to declare a method with no corresponding implementation. Then, subclasses could implement the methods that they were interested in. At runtime, an object would be asked if it had an implementation for those methods.

Some of the useful properties provided by the UIAccessibility protocol are:

accessibilityLabel

A short description of an element. For views with text, this is often the text that the view is displaying.

accessibilityHint

A short description of the result of interacting with the associated element. For example, the accessibility hint for a button that stops video recording might be Stop recording.

accessibilityFrame

The frame of the accessibility element. For UIView objects, this is equal to the frame of the view.

accessibilityTraits

Descriptions of the characteristics of the element. There are a lot of traits, and multiple traits can be used to describe the element. To see a list of all of the possible traits, look at the documentation for UIAccessibilityTraits.

accessibilityValue

A description of the value of an element, independent of its label description. For example, a UITextField will have an accessibility value that is the contents of the text field, and a UISlider will have an accessibility value that is the percentage that the slider has been set to.

Let’s take a look at how to implement accessibility via the Photorama application.

Reopen Photorama.xcodeproj. You are going to implement VoiceOver accessibility features. Currently, Photorama is not very accessible at all. Let’s start by considering how the application would look to someone with visual impairments.

Testing VoiceOver

The best way to test VoiceOver is with an actual device, so we strongly recommend using a device if you have one available.

If you do not have a device available, you can use the simulator. Begin by clicking on the Xcode menu and choosing Open Developer ToolAccessibility Inspector. Build and run the application; once the simulator is running the app, switch to the Accessibility Inspector and select the simulator from the target dropdown list (Figure 24.1).

Figure 24.1  Changing targets in the Accessibility Inspector

Screenshot of the Accessibility Inspector.

Once the target has been set to the simulator, click the Start inspection follows point button on the Accessibility Inspector’s toolbar. As you mouse over and navigate in the simulator, the Accessibility Inspector will provide information about whatever element has focus on the simulator’s screen. VoiceOver is not included on the simulator, but the information shown in the Accessibility Inspector is similar.

If you have a device, open Settings, choose GeneralAccessibilityVoiceOver, and finally turn on VoiceOver (Figure 24.2).

Figure 24.2  Enabling VoiceOver

Screenshot shows the enable toggle of VoiceOver.

There are a couple of ways to navigate with VoiceOver on. To start, slide your finger around the screen. Notice that the system speaks a description of whatever element your finger is currently over. Now tap the Back button in the top left corner of the screen that says Accessibility. The system will tell you that this element is the Accessibility - Back button. The system is reading you both the accessibilityLabel as well as what is essentially the accessibilityTraits.

Notice that tapping the Accessibility Back button does not take you back to the previous screen. To activate the selected item, double-tap anywhere on the screen. This corresponds to a single-tap with VoiceOver disabled. This will take you to the previous screen for Accessibility.

Another way to navigate is to swipe left and right on the screen. This will select the previous and next accessible elements on the screen, respectively. The VoiceOver row should be selected. Play around with swiping left and right to move the focus around the screen.

Swipe with three fingers to scroll. Note that to scroll, the scroll view or one of its subviews must be the currently focused element. Play around with single- and double-taps to select and activate items as well as using three fingers to scroll. This is how you will navigate with VoiceOver enabled.

One final useful gesture to know is how to enable Screen Curtain. Using three fingers, triple-tap anywhere on the screen. The entire screen will go black, allowing you to truly test and experience how your app will feel to someone with a visual impairment. Three-finger triple-tap anywhere again to turn Screen Curtain off.

Accessibility in Photorama

With VoiceOver still enabled, build and run Photorama on your device to test its accessibility. Once the application is running, drag your finger around the screen. Notice that the system is playing a dulled beeping sound as you drag over the photos. This is the system’s way of informing you that it is not able to find an accessibility element under your finger.

Currently, the PhotoCollectionViewCells are not accessibility elements. This is easy to fix.

Open PhotoCollectionViewCell.swift and override the isAccessibilityElement property to let the system know that each cell is accessible.

override var isAccessibilityElement: Bool {
    get {
        return true
    }
    set {
        super.isAccessibilityElement = newValue
    }
}

Now build and run the application. As you drag your finger across the photos, you will hear a more affirming beep and see each cell outlined with the focus rectangle (Figure 24.3). No description is spoken, but you are making progress.

Figure 24.3  Accessible cells

Screenshot of the photorama interface with several photograph thumbnails is shown. One of the thumbnails is selected.

Go back to PhotoCollectionViewCell.swift. You are going to add an accessibility label for VoiceOver to read when an item is selected. Currently, a cell knows nothing about the Photo that it is displaying, so add a new property to hold on to this information.

class PhotoCollectionViewCell: UICollectionViewCell {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var spinner: UIActivityIndicatorView!

    var photoDescription: String?

In the same file, override the accessibilityLabel to return this string.

override var accessibilityLabel: String? {
    get {
        return photoDescription
    }
    set {
        // Ignore attempts to set
    }
}

Open PhotoDataSource.swift and update collectionView(_:cellForItemAt:) to set the photoDescription on the cell.

func collectionView(_ collectionView: UICollectionView,
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let identifier = "PhotoCollectionViewCell"
    let cell =
        collectionView.dequeueReusableCell(withReuseIdentifier: identifier,
                                           for: indexPath) as! PhotoCollectionViewCell

    let photo = photos[indexPath.row]
    cell.photoDescription = photo.title

    return cell
}

Build and run the application. Drag your finger over the screen and you will hear the titles for each photo.

Currently there is no indication to users that they can double-tap to drill down to a specific photo. This is because the cells do not have any accessibility traits set.

In PhotoCollectionViewCell.swift, override the accessibilityTraits property to let the system know that a cell holds an image.

override var accessibilityTraits: UIAccessibilityTraits {
    get {
        return super.accessibilityTraits | UIAccessibilityTraitImage
    }
    set {
        // Ignore attempts to set
    }
}

You are combining any traits inherited from the superclass with the UIAccessibilityTraitImage. This is done using the | (or) operator to combine the two. Like many other things related to accessibility, this is a legacy of the past. In current, idiomatic Swift, this would be done using an OptionSet by passing the options as an array. But UIAccessibility does not support this syntax, so instead you are using | to group the options, which is how this is done in C and Objective-C.

Build and run the application. Notice that the new trait you added is spoken when you select a cell.

The remaining parts of the application are mostly accessible because they use standard views and controls. The only thing you need to update is the image view when drilling down to a specific Photo. You can customize many views’ accessibility information from within storyboards, and you will be able to do that for the image view.

Open Main.storyboard and navigate to the scene associated with the PhotoInfoViewController. Select the image view and open its identity inspector. Scroll to the bottom to the section labeled Accessibility. Check the box at the top of this section to enable accessibility for this image view and uncheck the box next to User Interaction Enabled (Figure 24.4).

Figure 24.4  Updating the accessibility options

In the identity inspector, the accessibility setting is enabled via a checkbox. In the several traits listed below this, the Image checkbox option is selected.

Open PhotoInfoViewController.swift and update viewDidLoad() to give the image view a more meaningful accessibility label.

override func viewDidLoad() {
    super.viewDidLoad()

    imageView.accessibilityLabel = photo.title

Build and run the application and navigate to a specific photo. You will notice that with this small addition, this entire screen is accessible. Finally, let’s turn our attention to the TagsViewController.

While still running the application, drill down to the TagsViewController. Add a tag to the table view if one is not already present. Select a row in the table and notice that VoiceOver reads the name of this tag; however, there is no indication to users that they can toggle the checkmark for each row, nor is the presence or absence of that checkmark communicated.

Open TagDataSource.swift and update the cell’s accessibility hint in tableView(_:cellForRowAt:).

func tableView(_ tableView: UITableView,
               cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell",
                                             for: indexPath)

    let tag = tags[indexPath.row]
    cell.textLabel?.text = tag.name

    cell.accessibilityHint = "Double tap to toggle selected"

    return cell
}

Build and run the application and marvel at its accessibility.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset