Build and Release

A continuous learner for experience and life.

Playing With iOS Background Multitasking

enter image description here

In the realm of the background multitasking, Apple has made some significant changes by exposing additional APIs since iOS7.​ iOS7 introduced some new background task handlings that help developers achieve some great user experiences, just like schedule the content update some spefice times, or allow the app to launch immediately whenever you send it a special push notification. They are called ‘background app fetch’ and ‘remote notifications’ correspondingly.

Let’s see how to configure the features:

iOS_Background_Configuration

To implement ‘background app fetch’, only two steps needed:

1. Set the minimum background fetch interval in the application:didFinishLaunchingWithOptions: method of the AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

2. Implement a new AppDelegate method when the App is background launched called application:performFetchWithCompletionHandler:

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"Background Fetch: Called in the background!");
    completionHandler(UIBackgroundFetchResultNewData);
}

To test the functionality, Xcode provide a very nice method under the menu ‘Debug’, called “Simulate Background Fetch”:

enter image description here

After selecting the submenu, Xcode will close your application in the simulator, sending it to the background, and call the application:performFetchWithCompletionHandler: that we added. You should have something printed in the console window now, like this (in the red rectangle):

enter image description here

There are more steps to implment “remote notification”, please refer to the following links: http://www.huffingtonpost.com/dulio-denis/ios-quick-read-implementi_2_b_5351099.html https://parse.com/tutorials/ios-push-notifications
https://developer.apple.com/notifications/

Here is the github repository for your reference as well: https://github.com/lifuzu/iOSBackground.git

Hope this can help you.

Written with StackEdit.

Comments