aMother
aMother

Reputation: 893

Sending Data To Server While App is Running in Background in iOS 7

I wish to send a little data (location Co-ordinates) to my server from the application which is running in background in iOS 7. I am using NSURLSessions for this purpose.
I am confused regarding the type of session I should use for this purpose :
Default Session: Can we create and use default sessions in background. If yes can it handle the network failures gracefully.
Background Session: Can I upload data as NSData object using background session.

PS: Some code will be really appreciated dealing with network failure in both cases.

Upvotes: 6

Views: 4624

Answers (1)

Dhilip
Dhilip

Reputation: 498

I would advise you to use Background session.

You may use Default session or a regular NSURLConnection with a background expiry identifier with a max of 10 minutes to complete your tasks

Background Task Identifier Documentation

https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:

Using NSURLConnection in background task ( you also get iOS 6 support)

NSURLConnection best practise when enter background

By the new standards you can use Background Session to handle downloads/ server uploads.

Apple's documentation on handling background downloads

https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW1

A Simple project from Apple illustrating the technique

https://developer.apple.com/library/ios/samplecode/SimpleBackgroundTransfer/Listings/SimpleBackgroundTransfer_APLViewController_m.html#//apple_ref/doc/uid/DTS40013416-SimpleBackgroundTransfer_APLViewController_m-DontLinkElementID_7

Hope this helps you !

Upvotes: 7

Related Questions