1

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.

Aqeel Ahmad
  • 709
  • 7
  • 20
ronghester
  • 23
  • 2
  • `application(_:handleEventsForBackgroundURLSession:completionHandler:)` Do you have this implemented? – nanibir Jan 07 '20 at 16:04
  • Yes that is implemented in appdelegate.m class. On the device where the download is failing over cellular data; none of the delegate methods are getting called. For example the progress bar is getting updated in one of the delegate method; but since no data is received the progress bar is not moving at all. This is the method implementation you requested. `-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler { self.backgroundTransferCompletionHandler = completionHandler; }` – ronghester Jan 08 '20 at 12:43

0 Answers0