background download
Background download is failing over cellular data. Strangely it is only happening on few devices. we checked the OS of the device and they have the latest 13.x update. below code we are using to download data in background mode. We have already set “App Transport Security Settings —> Allow Arbitrary Loads —> YES”. In some device over cellular network none of the delegate methods are getting called.
*********************************************************************
#import "MyAccountViewController.h"
@interface MyAccountViewController ()<NSURLSessionDelegate>
{
NSURLSessionConfiguration *sessionConfiguration;
}
@property (nonatomic, strong) NSURLSession *backgroundSession;
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@end
- (void)viewDidLoad
{
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @“testdownloadIdentifier];
self.backgroundSession = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate: self
delegateQueue:[NSOperationQueue mainQueue]];
sessionConfiguration.discretionary = false;
}
- (IBAction)downloadBtnTapped:(id)sender
{
NSURL *sourceUrl = [NSURL URLWithString:@“http://files.infogridpacific.com/ss/igp-twss.epub”];
self.downloadTask = [self.backgroundSession downloadTaskWithURL: sourceUrl];
[self.downloadTask resume];
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
NSLog(@"INSIDE_didWriteData ");
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
NSLog(@"INSIDE_didFinishDownloadingToURL ");
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSInteger code = [(NSHTTPURLResponse *)task.response statusCode];
NSLog(@"INSIDE_didCompleteWithError code : %ld",code);
}
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
[self.backgroundSession getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([downloadTasks count] == 0) {
if (appDelegate.backgroundTransferCompletionHandler != nil) {
void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;
appDelegate.backgroundTransferCompletionHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionHandler();
}];
}
}
}];
}
*********************************************************************
Please provide some suggestions on how to successfully download file in background mode over cellular data.