Multitasking in iOS 7

23
Multitasking in iOS 7 Mickaël Rémond <[email protected]> Universal Push Notification Platform

description

Implementing and understanding iOS 7 addition for multitasking and background tasks.

Transcript of Multitasking in iOS 7

Page 1: Multitasking in iOS 7

Multitasking in iOS 7

Mickaël Rémond <[email protected]>

Universal Push Notification Platform

Page 2: Multitasking in iOS 7

Pre-iOS 7 options for background modes

Universal Push Notification Platform

Previously you had access to the following options: !

Audio playback Location update Task completion (up to 10 minutes) Newsstand download Voice-over-IP (VoIP) services Accessory or CoreBluetooth communication !

Specific limitations for each of them

Page 3: Multitasking in iOS 7

Change in background tasks handling (1/3)

Universal Push Notification Platform

Apps will get scheduled when device is active and not keep it awake They still get up to 10 minutes run time, not guaranteed to be continuous !

WWDC 204

Page 4: Multitasking in iOS 7

Change in background tasks handling (2/3)

Universal Push Notification Platform

// AppDelegate UIBackgroundTaskIdentifier background_task; !- (void)applicationDidEnterBackground:(UIApplication *)application { if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { NSLog(@"Multitasking Supported"); if (background_task == UIBackgroundTaskInvalid) { background_task = [application beginBackgroundTaskWithExpirationHandler:^ { NSLog(@"Background task expiration\n"); //Clean up code. Tell the system that we are done. [application endBackgroundTask: background_task]; background_task = UIBackgroundTaskInvalid; }]; //To make the code block asynchronous dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //### background task starts NSLog(@"Running in the background\n"); while(TRUE) { NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]); [NSThread sleepForTimeInterval:1]; //wait for 1 sec } //#### background task ends //Clean up code. Tell the system that we are done. [application endBackgroundTask: background_task]; background_task = UIBackgroundTaskInvalid; }); } else { NSLog(@"Multitasking Not Supported"); } } } !

Page 5: Multitasking in iOS 7

Change in background tasks handling (3/3)

Universal Push Notification Platform

On iOS 6, starts with 10 min: 2013-11-14 08:59:54.165 Background1[149:1103] Background time Remaining: 599.981943 !!On iOS 7, starts with 3 min: 2013-11-14 09:02:33.182 Background1[6315:1803] Background time Remaining: 179.942678

!This is a big change for many apps using background tasks. !I never seem task being scheduled discontinously … Yet.

Page 6: Multitasking in iOS 7

iOS 7 new background modes

Universal Push Notification Platform

With iOS 7 you have three new modes: !

Background fetching Remote notification Background transfers !

All modes keep Apple and users in control to preserve battery life

Page 7: Multitasking in iOS 7

Project *info.plist: select background modes

Universal Push Notification Platform

Two new modes available: !

fetch (App downloads content from the network) remote-notification (App download content in response to push notifications)

Page 8: Multitasking in iOS 7

Background fetch

Universal Push Notification Platform

Page 9: Multitasking in iOS 7

Background fetching: close control

Universal Push Notification Platform

!iOS stays in control: “Application launched opportunistically”

Coalesced across applications means scheduled based on usage pattern sensitive to energy and data usage

!Users stay in control:

Force close disable background fetch Dedicated setting panel

Page 10: Multitasking in iOS 7

Background fetching: user control

Universal Push Notification Platform

Preferences -> General -> Background App Refresh

Page 11: Multitasking in iOS 7

Background fetching: usage patterns

Universal Push Notification Platform

WWDC 204

Page 12: Multitasking in iOS 7

Background fetching: App Delegate

Universal Push Notification Platform

New delegate method on UIApplication is called - (void)application:(UIApplication *)application! performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult! result))completionHandler;!

!Call the completion handler when fetch is complete to let iOS 7 learn about data availability patterns:

UIBackgroundFetchResultNewData!UIBackgroundFetchResultNoData!UIBackgroundFetchResultFailed

Page 13: Multitasking in iOS 7

Background fetching: Limit server load

Universal Push Notification Platform

At application launch: !Define the minimum fetch interval to limit server load: - (void)setMinimumBackgroundFetchInterval:(NSTimeInterval)minInterval;!

!Use constants: const NSTimeInterval UIApplicationBackgroundFetchIntervalMinimum!const NSTimeInterval UIApplicationBackgroundFetchIntervalNever!

!More likely custom values (in seconds)

Page 14: Multitasking in iOS 7

Background fetching: Tips

Universal Push Notification Platform

Simulate background fetch for testing Do not forget simulator has an option to manually trigger background fetch

Limit operation to bare minimum You have at most 30 seconds of time to perform download operation

Use NSURLSession to benefit from background download infrastructure Track background fetches per user on your server to discover patterns

Page 15: Multitasking in iOS 7

Remote notifications

Universal Push Notification Platform

Page 16: Multitasking in iOS 7

Remote notifs: Dev and Apple control

Universal Push Notification Platform

Developers control: content-available set to 1 in APNS payload to tell the application content is available.

!Apple stays in control:

Silent notification (without sound / alert / badge) can be delayed: Battery saving Normal notifications are send immediately

Page 17: Multitasking in iOS 7

Remote notifs: App Delegate

Universal Push Notification Platform

New delegate method on UIApplication is called - (void)application:(UIApplication *)application! didReceiveRemoteNotification:(NSDictionary *)userInfo! fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;!

!Call the completion handler when download is complete to let iOS 7 learn about data availability patterns:

UIBackgroundFetchResultNewData!UIBackgroundFetchResultNoData!UIBackgroundFetchResultFailed!

Page 18: Multitasking in iOS 7

Remote notifs: Tips

Universal Push Notification Platform

!Limit operation to bare minimum

You have at most 30 seconds of time to perform download operation !

Use NSURLSession to benefit from background download infrastructure

Page 19: Multitasking in iOS 7

Background transfers

Universal Push Notification Platform

Page 20: Multitasking in iOS 7

Background transfers: Usage

Universal Push Notification Platform

!Used for data download and upload !Used for granting iOS control over background operation !

Does not obsolete AFNetworking especially if you need parsing / serialization !Quite a large piece of code with many options: Take the time to learn them, they are powerful

Page 21: Multitasking in iOS 7

Background transfers: Nice features

Universal Push Notification Platform

!Download / upload tasks in background !Discretionary setting to save battery !Limit download to Wifi only

Page 22: Multitasking in iOS 7

Background transfers: Tips

Universal Push Notification Platform

!Take the time to explore all the options / cases. To implement complex cases, data will flow through multiple paths in code !Not “magic”: You still have to handle network failure and errors, resume on the next chunk

Do not forget there is a server on the back that will timeout at some point !

Take it as a great toolbox to handle advanced cases

Page 23: Multitasking in iOS 7

References

Universal Push Notification Platform

Talk example code https://github.com/boxcar/talk-code-examples/tree/master/background-ios7 !

Background Modes in iOS Tutorial (pre-iOS 7) http://www.raywenderlich.com/29948/backgrounding-for-ios !

WWDC 2013 Session 204: What’s new with multitasking.