iOS Downloading multiple files in the background

764 Views Asked by At

I have read few articles but could not find what I was looking for so here's my query.

I am downloading few files from the server and there is a case where the user locks his screen, in this case the ios device loses its network connectivity and the file sync fails.

I have read few article on NSURLSession but it is available from iOS 7 onwards and the app I am working on supports from iOS 6 and later.

So is there a way where I can download 20 or 30 files in the background or when the user hits the lock screen in a generic fashion without having to worry about which OS version I am supporting.

As of now I have read that we have 30 seconds to perform network activity so is there a limitation to the number of server calls in these 30 seconds?

About my code, I am having a class named as DownloadFiles which calls a service and the service returns me an array of fileURL and using NSData I am fetching these files and saving them in the doc directory, so while implementing the background call thing do I need to pass the index of my array which will detect the current file which is downloading and then carry on from the next index.

  for(NSDictionary *dict in filearray) {
     NSString *fileURL = [[dict valueForKey:@"FileURL"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSData *fileData  = [NSData dataWithContentsOfURL:theFileURL];

     if (fileData.length==0 || fileData==nil || theFileURL==nil) {
        NSLog(@"empty file URL = %@",theFileURL);
     }
     if (fileData.length!=0){
        BOOL savefile = [fileData writeToFile:[HTML_SERVER_FILES stringByAppendingPathComponent:[dict valueForKey:@"FileName"]] atomically:YES];
        if (savefile!=YES) {
           NSLog(@"Not saved file = %@",theFileURL);
        }else{
           NSLog(@"file saved at path %@",HTML_SERVER_FILES);
        }

        fileData = nil;
    }
}

Please let me know what needs to be done in this case.

0

There are 0 best solutions below