Slide 1

Slide 1 text

Pawel Rusin (@RusinPaw) iOS dev at Cookpad Tokyo iOS Meetup 2016-08-13 Background execution in iOS

Slide 2

Slide 2 text

Agenda ● Motivation ● App lifecycle ● Background execution modes – App leaves the foreground – App enters the background – App enters the foreground ● Summary

Slide 3

Slide 3 text

Motivation

Slide 4

Slide 4 text

I want to make an app that does X

Slide 5

Slide 5 text

I want to make an app that does X (In the background)

Slide 6

Slide 6 text

How in iOS can I do X?

Slide 7

Slide 7 text

How in iOS can I do X? (In the background)

Slide 8

Slide 8 text

Is it OK in iOS to do X?

Slide 9

Slide 9 text

Is it OK in iOS to do X? (In the background)

Slide 10

Slide 10 text

https://developer.apple.com/app-store/review/rejections/

Slide 11

Slide 11 text

How in iOS can I do X? (In the background)

Slide 12

Slide 12 text

Google

Slide 13

Slide 13 text

Stack overflow

Slide 14

Slide 14 text

Google

Slide 15

Slide 15 text

● What can be done? ● What cannot be done? ● What shouldn't be done?

Slide 16

Slide 16 text

App lifecycle

Slide 17

Slide 17 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 18

Slide 18 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 19

Slide 19 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 20

Slide 20 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 21

Slide 21 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 22

Slide 22 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 23

Slide 23 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 24

Slide 24 text

App lifecycle Inactive Active Background Foreground Background

Slide 25

Slide 25 text

App lifecycle Not running Inactive Active Background Suspended Foreground Background

Slide 26

Slide 26 text

App leaves the foreground

Slide 27

Slide 27 text

Not running Inactive Active Background Suspended Foreground Background App leaves the foreground

Slide 28

Slide 28 text

Executing Finite-Length Tasks

Slide 29

Slide 29 text

Executing Finite-Length Tasks When the user is not actively using your app, the system moves it to the background state. For many apps, the background state is just a brief stop on the way to the app being suspended. Suspending apps is a way of improving battery life it also allows the system to devote important system resources to the new foreground app that has drawn the user’s attention. An app in the background state should do as little work as possible. Apps that request time to process specific types of events should process those events and return control back to the system as quickly as possible. App Programming Guide for iOS (background execution) UIAppDelegate documentation

Slide 30

Slide 30 text

Executing Finite-Length Tasks When the user is not actively using your app, the system moves it to the background state. For many apps, the background state is just a brief stop on the way to the app being suspended. Suspending apps is a way of improving battery life it also allows the system to devote important system resources to the new foreground app that has drawn the user’s attention. An app in the background state should do as little work as possible. Apps that request time to process specific types of events should process those events and return control back to the system as quickly as possible. App Programming Guide for iOS (background execution) UIAppDelegate documentation

Slide 31

Slide 31 text

Executing Finite-Length Tasks https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html

Slide 32

Slide 32 text

Executing Finite-Length Tasks Icon made by Silviu Runceanu from www.flaticon.com

Slide 33

Slide 33 text

Executing Finite-Length Tasks Downloading Content in the Background App leaves the foreground

Slide 34

Slide 34 text

App leaves the foreground UIApplication.sharedApplication().begi nBackgroundTaskWithExpirationHandler { // perform a finite tast } UIApplication().sharedApplic ation.endBackgroundTask()

Slide 35

Slide 35 text

● Passes the download task to a separate process ● Requires specific type of NSURLSession – background session ● Gwendolyn Weston: “While Your App Was Sleeping: Background Transfer Services” Downloading Content in the Background

Slide 36

Slide 36 text

App enters the background

Slide 37

Slide 37 text

App enters the background Not running Inactive Active Background Suspended Foreground Background

Slide 38

Slide 38 text

Audio and AirPlay Location updates Voice over IP Newsstand downloads External accessory communication Uses Bluetooth LE accessories Acts as a Bluetooth LE accessory Background fetch Remote notifications launch App enters the background

Slide 39

Slide 39 text

App enters the background

Slide 40

Slide 40 text

App enters the background trigger

Slide 41

Slide 41 text

App enters the background trigger Audio buffer empty Location update Download task finished Time elapsed Silent push data from a peripheral

Slide 42

Slide 42 text

Audio and AirPlay Location updates Voice over IP Newsstand downloads External accessory communication Uses Bluetooth LE accessories Acts as a Bluetooth LE accessory Background fetch Remote notifications launch App enters the background

Slide 43

Slide 43 text

Audio and AirPlay Location updates Voice over IP Newsstand downloads External accessory communication Uses Bluetooth LE accessories Acts as a Bluetooth LE accessory Background fetch Remote notifications launch App enters the background

Slide 44

Slide 44 text

● Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content. ● Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. ● The system must balance your app’s need to fetch content with the needs of other apps and the system itself. Background fetch https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgramming Guide/BackgroundExecution/BackgroundExecution.html

Slide 45

Slide 45 text

Background fetch

Slide 46

Slide 46 text

Background fetch UIApplication.sharedApplication().setMinimumBackgro undFetchInterval(24*60*60) (UIApplicationBackgroundFetchIntervalMinimum)

Slide 47

Slide 47 text

Background fetch func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { //fetch sth if error != nil { return completion(UIBackgroundFetchResultFailed) } else if newData { return completion(UIBackgroundFetchResultNewData) } else { return completion(UIBackgroundFetchResultNoData) } }

Slide 48

Slide 48 text

Remote (silent) push { ... content-available: 1, ... }

Slide 49

Slide 49 text

Remote (silent) push

Slide 50

Slide 50 text

Remote (silent) push func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { //do sth }

Slide 51

Slide 51 text

● Specific tasks of a short execution time ● Know your API (and your limitations) ● Don't be greedy App enters the background

Slide 52

Slide 52 text

App enters the foreground

Slide 53

Slide 53 text

Not running Inactive Active Background Suspended App enters the foreground USER CONSENT

Slide 54

Slide 54 text

Remote notifications Local Notification App enters the foreground

Slide 55

Slide 55 text

App enters the foreground let localNotification = UILocalNotification() localNotification.fireDate = NSDate(timeIntervalSinceNow: 300) // 5mins localNotification.alertBody = "stand up!" localNotification.timeZone = NSTimeZone.defaultTimeZone() UIApplication.sharedApplication().scheduleLocalNotification(l ocalNotification)

Slide 56

Slide 56 text

App enters the foreground func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { //do sth }

Slide 57

Slide 57 text

● One way of “reminding” user about the app ● Alarms, motivational apps ● Be nice! App enters the foreground

Slide 58

Slide 58 text

● What can be done? ● What cannot be done? ● With great power comes great responsibility Summary

Slide 59

Slide 59 text

● https://developer.apple.com/library/ios/documentation/iPhone/Co nceptual/iPhoneOSProgrammingGuide/BackgroundExecution/Ba ckgroundExecution.html ● https://developer.apple.com/library/ios/documentation/UIKit/Refer ence/UIApplicationDelegate_Protocol/ ● https://realm.io/news/gwendolyn-weston-ios-background- networking/ ● https://blog.newrelic.com/2016/01/13/ios9-background-execution/ References