Background Fetch iOS Tutorial

The background Fetch API allows an app to get updated content when the app isn't running in the foreground. iOS intelligently schedules the background fetch events based on your app usage so when you open your app the content is always up to date. In this app we will simulate background fetching to get the current time into our app. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOSBackgroundFetchTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

To enable the abiltiy of background fetching for our application we need to go to the project settings and select Capabilties. Set the Background Modes switch to On and check the Background fetch option.

Go to the Storyboard and drag a Label to the main view. Set the title of the button to Not Updated.. The Storyboard should look like this.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Label to the ViewController class and create the following Outlet

Go to the ViewController.swift file and add a Date property to the ViewController class

var currentTime: Date?

Add the following method

func updateTime() {
  currentTime = Date()
  let formatter = DateFormatter()
  formatter.timeStyle = .short

  if let currentTime = currentTime {
    timeLabel.text = formatter.string(from: currentTime)
  }
}

This method will be called when the background fetch starts. This will display the current time. Go to the AppDelegate.swift file and change the application(_:didFinishLaunchingWithOptions:) method to

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Override point for customization after application launch.
  UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)

  return true
}

The recommended value for minimumBackgroundFetchInterval is backgroundFetchIntervalMinimum, which means iOS itself will figure out how often it needs to fetch data on the background. When a background fetch occurs, iOS starts the app and then makes a call to the application delegate method application(_:performFetchWithCompletionHandler:).

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  if let viewController = window?.rootViewController as? ViewController {
    viewController.updateTime()
  }
}

The updateTime method will be called. Build and Run the project. Press the home button of the iOS Simulator and in Xcode select the Simulate Background Fetch option in the Debug Menu. Go back to the iOS Simulator and start the application icon. Now the current time is displayed.

You can download the source code of the IOSBackgroundFetchTutorial at the ioscreator repository on Github.