NSURLSession + Multitasking + iOS 7

In my application, I try to get new content and update it, even if my application is in standby mode. For this, I want to use the new multitasking function of iOS 7.

First of all, I turned on the background selection by adding the fetch key to UIBackgroundModes in Info.plist.

Then I set the minimum background fetch interval in AppDelegate:

 [app setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum] 

And the completion of the implementation:

 (void) application : (UIApplication *)application performFetchWithCompletionHandler:(void(^) (UIBackgroundFetchResult))completionHandler 

Now I have to use NSURLSession to achieve what I want to do: - check for new image files on the server; - if YES, download it - a push notification informing the user about the availability of new content.

I will try something like this in performFetchWithCompletionHandler: ::

 NSURLSession* session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration backgroundSessionConfiguration:@"download"]]; [session downloadTaskWithRequest:<#(NSURLRequest *)#> completionHandler:<#^(NSURL *location, NSURLResponse *response, NSError *error)completionHandler#>] 

But I do not like NSURLSession. If anyone has sample code to use it, I will be very happy to try it.

Tai, Pebi

+4
source share
2 answers

here is a sample code: https://developer.apple.com/downloads/index.action download the iOS_SimpleBackgroundTransfer sample.

+3
source

In your performFetchWithCompletionHandler :, check to see if new images are available using NSURLRequest / NSURLConnection, as you wish.

If you have images to upload, create an NSURLSession using backgroundSessionConfiguration to load the images.

0
source

Source: https://habr.com/ru/post/1501306/


All Articles