How to get backgroundSession response NSURLSessionUploadTask

I implemented NSURLSession, which runs in the background (so that it can continue the task using the system daemon even when the application is paused). The problem is that

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler 

never called. I need to know the status of the response so that I can correctly handle the download error. According to another post, an Apple engineer said that this delegate method is not called when the session is in the background to prevent the application from waking up. Any suggestion on how to solve this problem? The last URLSession delegation method that is called in my situation:

 -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data 
+5
ios ios7 nsurlsession
Mar 18 '14 at 17:58
source share
1 answer

The URLSession:task:didCompleteWithError: NSURLSessionTaskDelegate should be called when the download completes. Refer to the task.response object, which should be an NSHTTPURLResponse object.




I am sure you are doing this, but the standard components of the background loading task are:

  • Make background session:

     NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.domain.app"]; NSURLSession *session = [NSURLSession sessionWithConfiguration:backgroundConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
  • Use the NSURLSession method uploadTaskWithRequest:fromFile: method:

     NSURLSessionTask *task = [session uploadTaskWithRequest:request fromFile:fileURL]; [task resume]; 

    With a background session, you should:

    • Use NSURLSessionUploadTask ;

    • Use a file-based version (you cannot use a version based on NSData );

    • Using delegate-based transfers cannot use data tasks; (b) NSData execution of NSURLSessionUploadTask ; nor (c) execution of an NSURLSessionUploadTask termination NSURLSessionUploadTask .

  • When loading tasks, do not call setHTTPBody from NSMutableRequest . With load tasks, the request body cannot be in the request itself.

  • Make sure that you apply the appropriate methods NSURLSessionDelegate , NSURLSessionTaskDelegate .

  • Be sure to implement application:handleEventsForBackgroundURLSession: in the application deletion (so that you can write the completionHandler that you call in URLSessionDidFinishEventsForBackgroundURLSession ).

+10
Mar 18 '14 at 18:26
source share



All Articles