I was trying to use the new ios7 background transfer api to upload some photos to a server. what it happened now is 1) we upload the bytes to s3 2) call a service api to 'complete' the upload
i looked at this doc and it seems background NSURLSession doesn't support 'data' task. does that mean i can't do the step 2 in background after the actual upload is done?
If you want a simpler solution than repurposing NSURLSessionDownloadTask
for your "completed" API call, you can round trip a quick http call during the callback in:
-URLSession:task:didCompleteWithError:
For S3 with background task see my answer here
Another good resource is the apple sample code here and look for "Simple Background Transfer"
You have to create an upload task and upload from local file. While your app is running the NSURLSessionTaskDelegate delegate methods will be called on completion specifically at upload completion:
/* Sent as the last message related to a specific task. Error may be
* nil, which implies that no error occurred and this task is complete.
*/
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error;
If your app went to the background or even was killed by iOS (and not by the user), your app would be waken up with
URLSessionDidFinishEventsForBackgroundURLSession
And then your NSURLsession delegate method would be called
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
The way you should build it is as follow, in your appDelegate add
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
/*
Store the completion handler. The completion handler is invoked by the view controller's checkForAllDownloadsHavingCompleted method (if all the download tasks have been completed).
*/
self.backgroundSessionCompletionHandler = completionHandler;
}
Then in your controller/model class add
/*
If an application has received an - application:handleEventsForBackgroundURLSession:completionHandler: message, the session delegate will receive this message to indicate that all messages previously enqueued for this session have been delivered. At this time it is safe to invoke the previously stored completion handler, or to begin any internal updates that will result in invoking the completion handler.
*/
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
APLAppDelegate *appDelegate = (APLAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.backgroundSessionCompletionHandler) {
void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
appDelegate.backgroundSessionCompletionHandler = nil;
completionHandler();
}
NSLog(@"All tasks are finished");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With