iOS 7 provides multitasking API to fetch/download the content regularly in background. Using iOS background fetch API, your App can register with the system and get callback to download the content in background.

 

Prerequisites:

1). You need add UIBackgroundModes key with the fetch value in your app’s Info.plist file

Screen Shot 2013-06-11 at 6.20.12 PM

2). You need to implement application:performFetchWithCompletionHandlerin app delegate

  1. -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  2. {
  3.  
  4.         //Tell the system that you ar done.
  5.         completionHandler(UIBackgroundFetchResultNewData);
  6. }
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{

        //Tell the system that you ar done.
        completionHandler(UIBackgroundFetchResultNewData);
}

 

How to initialize:

  1. [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:minimumBackgroundFetchInterval];
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:minimumBackgroundFetchInterval];

If minimumBackgroundFetchInterval isUIApplicationBackgroundFetchIntervalMinimum, then system will decide when to call performFetchWithCompletionHandler method.
If minimumBackgroundFetchInterval isUIApplicationBackgroundFetchIntervalNever, then system will never call callback handler.
This API is not like a timer task, system will decide when to call the handler depending on many constraints.so if you set timeInterval to 2.00f( 2secs), handler is called for every 2+(minimum 2)secs.

 

Restrictions:
When performFetchWithCompletionHandler method is called, app has up to 30 seconds of time to perform the download operation and call the specified completion handler block. If the completion handler is not called in time, then your app will be suspended.

Example:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
  4. return YES;
  5. }
  6.  
  7. -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  8. {
  9. NSLog(@"########### Received Background Fetch ###########");
  10. //Download  the Content .
  11.  
  12. //Cleanup
  13. completionHandler(UIBackgroundFetchResultNewData);
  14.  
  15. }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"########### Received Background Fetch ###########");
//Download  the Content .

//Cleanup
completionHandler(UIBackgroundFetchResultNewData);

}

 

Testing Background fetch in Simulator?
You can test background fech in two methods.
1). Run your app in simulator. In XCode Menu, Go to “Debug” => “Simulate Background Fetch”.
after this performFetchWithCompletionHandler() is called.
Simulate Background Fetch

 

2). In XCode Menu, Go to “Product” => “Scheme” => “Edit Scheme”
enable the option “Background Fetch“. Rebuild and Run you app. performFetchWithCompletionHandler() is called while launching the app

Simulate Background Fetch while launching