Working on an IOS9 app that is doing a background URLSession in a controller that is a NSURLSessionDelegate. Here is how I start it:
self.session_data = [[NSMutableData alloc] init];
NSURL *url = [NSURL URLWithString:src];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionConfiguration *backgroundConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"myBackgroundSessionIdentifier"];
self.session = [NSURLSession sessionWithConfiguration: backgroundConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
self.download = [self.session dataTaskWithRequest: request ];
[self.download resume];
So far so good. I implement the three delegate methods. 'didReceiveData' is called first and I store the data.
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data{
NSLog(@"%s",__func__);
[self.session_data appendData:data];
}
Right after that 'didCompleteWithError' is called. The 'completionHandler' handler is never called.
What is confusing about 'didCompleteWithError' message is that the actual error object is nil. I have seen some similar unanswered questions. I am not leaving the controller/view while loading. Do I need to move that functionality into AppDelegate?
Apple doc said that didCompleteWithError report only client side error, otherwise is nil:
"Server errors are not reported through the error parameter. The only errors your delegate receives through the error parameter are client-side errors, such as being unable to resolve the hostname or connect to the host."
This is the link to the documentation.
If you want to check other errors like session's errors you have to implement session protocol delegate
- URLSession:didBecomeInvalidWithError:
For more details, see this answer
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