How to implement multitasking in iOS?
Up until iOS 8, Apple didn’t allow applications to run in a multitasking environment, except for during very specific and limited amounts of time: that is, when an application was sent to the background, the application only had a few minutes to complete certain tasks, such as finalizing network requests. Apple was extremely careful to limit what they allowed to happen in the background without any time limits. Indeed, developers were only ever able to play audio, receive location updates, or provide VoIP services without time restrictions, while an app was running in the background.
iOS 9 introduces new opportunities to run multiple, active applications, concurrently. To support multitasking, developers just need to apply some of the latest technologies, most of which have been available since Xcode 6.
Allowed multitasking modes
iOS 9 supports applications multitasking in two different ways: Split View and Picture in Picture. In addition, apps need to be compatible with Slide Over; that is, they should be configured to appear in the side menu that the user slides from the right edge of the screen (or the left, in RTL languages). In order to be compatible, developers need to set up their projects as follows:
- Set the base SDK to “Latest iOS”
- Provide a single storyboard file for the launch screen (instead of a .png image in multiple sizes).
- Support all four interface orientations (Portrait and Landscape, both standard and upside down)
Two applications can run at the same time sharing the screen space. In this scenario, the left side application is called the Primary App, and the right side app is called the Secondary App. The Primary App has some advantages over the Secondary App:
- It owns the status bar;
- It is able to work using a second physical screen;
- It can work with Picture in Picture automatic invocation;
- It can occupy two-thirds of the screen (in landscape orientation, only).
Users are able to resize the Primary and Secondary App spaces by dragging the separator view. The Secondary App can fill either one third or one half of the screen, while the Primary App can take up either two thirds or half of the screen.
What’s needed to support Split View?
iOS 9 makes use of Size Classes to determine how to lay out an application in a split view environment. This diagram shows which Size Classes iOS 9 uses to render the application’s layout:
In order to support Split View, developers just need to support Slide Over and use Size Classes in their storyboards or Xibs. That way, iOS 9 will be able to render the UI correctly as a user moves the slider. Most of today’s apps already use Size Classes, so only minor modifications should be required to add Multitasking support to apps.
Picture in Picture (PiP)
If an application plays videos, developers can add support for Picture in Picture. However, Apple doesn’t recommend supporting Picture in Picture, if the video feature is not a key part of the app. For example, if your app includes an introduction or demo video, using Picture in Picture wouldn’t be recommended; but, if the application is an educational app, that shows course videos, it would be great to support PiP.
What’s needed to support PiP?
First of all, the app should support Slide Over. After that, the following configuration is required:
- In the Capabilities view, enable Background Modes and select “Audio, AirPlay and Picture in Picture” mode.
- Ensure your app’s audio session uses an appropriate category, such as AVAudioSessionCategoryPlayback.
In addition, the application should use one of the following classes for video playback:
- AVPlayerViewcontroller class: automatically displays a PiP button.
- AVPictureInPictureController class: Available in AVKit, can be used with the AVPlayerLayer class from AVFoundation.
- WKWebView class: Available in the WebKit framework, and supports PiP in iOS 9 by default.
If a video is being played using one of those components, when the user presses the home button or taps a notification that takes them into another app, the video will continue playing in PiP mode. In addition, when a video moves to PiP playback, the application will potentially keep running in the background. It’s important for developers to avoid using unnecessary resources while an app is running in the background, such as in a multitasking environment: if the app that’s running in background is consuming too much resource, iOS will automatically terminate it.
Adding support for multitasking is very straightforward if the latest technologies were used during app development. However, developers must take care to optimize their apps, in order to avoid performance issues. When two applications run at the same time, they share CPU and memory usage. In order to help optimize resources, Apple has also introduced new technologies, such as App Thinning and Slicing.
We always recommend (and use) the latest Apple technologies, as they bring significant advantages to new versions of iOS or Xcode. Size Classes and Asset Catalogs might just seem like ways to organise and design your application in a clearer manner, but with Xcode 7 and iOS 9 they acquire a central role in supporting Multitasking and other new enhancements, all of which you can access without expending a huge amount of extra effort.