I have a ebook related app live on app store. This app downloads big files (ranging from ~100MB - 1GB) in background mode. Out of 100 users 10 users have reported problems with the download when they are downloading over cellular data. 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.
#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();
}];
}
}
}];
}
We have tried following 1. App Transport Security Settings —> Allow Arbitrary Loads —> YES 2. Tried downloading in foreground (it works perfectly on the affected devices) 3. Tried discretionary values YES/NO both but it does not have any effect.
In the devices where download is failing the delegate methods are not getting called.
Please suggest if there is any alternate way to download big files over cellular data.