Documentation Archive

Developer

Energy Efficiency Guide for iOS Apps

On This Page

Minimize Networking

Network operations may be unavoidable and essential to your app. In many cases, however, networking can be minimized by adhering to some general guidelines.

Reduce Data Sizes

Network transactions should be as small as possible to reduce overhead.

Reduce Media Quality and Size

If your app uploads, downloads, or streams media content, lower quality and smaller sizes reduce the amount of data being sent and received. Some apps let the user specify the quality and size. For example, when emailing a photo, Mail lets the user send a scaled version of the image at small, medium, or large size. The smallest size is the most energy efficient.

Compress Data

Use compression algorithms to compact data as much as possible before sending or receiving it.

Avoid Redundant Transfers

Your app shouldn’t repeatedly download the same data.

Cache Data

Use caching to store infrequently updated data locally. Redownload the data only when it has changed or the user requests it. The NSURLCache and NSURLSession APIs can be used to implement in-memory and on-disk caches for URL request data.

Use Pausable and Resumable Transactions

Network conditions fluctuate, and signal loss can be a regular occurrence. Be prepared to resume interrupted transactions so the same content isn’t downloaded multiple times. In some cases, it makes sense to let users pause downloads and resume them later. For example, iOS lets users pause app downloads by tapping on a partially downloaded app icon.

The NSURLSession API lets you implement pause and resume functionality, without implementing caching.

Handle Errors

Don’t attempt to perform network operations when the network is unavailable.

Check Signal Conditions

If network operations fail, use the SCNetworkReachability API to to see whether the host is available. If there are signal problems, alert the user or defer work until the host is available again.

To determine whether a host is reachable, check for the absence of the kSCNetworkReachabilityFlagsReachable reachability flag, as demonstrated in Listing 9-1.

Listing 9-1Checking the availability of a host

Objective-C

  1. #import "SystemConfiguration/SCNetworkReachability.h"
  2. ...
  3. // Create a reachability object for the desired host
  4. NSString *hostName = @"someHostName";
  5. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
  6. // Create a place in memory for reachability flags
  7. SCNetworkReachabilityFlags flags;
  8. // Check the reachability of the host
  9. SCNetworkReachabilityGetFlags(reachability, &flags);
  10. // Release the reachability object
  11. CFRelease(reachability);
  12. // Check to see if the reachable flag is set
  13. if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
  14. // The target host is not reachable
  15. // Alert the user or defer the activity
  16. }

Swift

  1. import SystemConfiguration
  2. ...
  3. // Create a reachability object for the desired host
  4. let hostName = "someHostName"
  5. let reachability = SCNetworkReachabilityCreateWithName(nil, (hostName as NSString).UTF8String).takeRetainedValue()
  6. // Create a place in memory for reachability flags
  7. var flags: SCNetworkReachabilityFlags = 0
  8. // Check the reachability of the host
  9. SCNetworkReachabilityGetFlags(reachability, &flags)
  10. // Check to see if the reachable flag is set
  11. if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
  12. // The target host is not reachable
  13. // Alert the user or defer the activity
  14. }

Provide an Escape Route

Don’t wait forever for a response from the server that never comes. Let the user cancel long-running or stalled network operations, and set appropriate timeouts so your app doesn’t keep connections open needlessly.

Use Retry Policies

If a transaction fails, try again when the network becomes available. Use the SCNetworkReachability API to determine or be notified when the network is available again.