|
@@ -57,6 +57,113 @@
|
|
|
}
|
|
|
|
|
|
|
|
|
+- (void)downloadURL:(NSURL *)url toLocation:(NSString *)dlLocation
|
|
|
+ progress:(DownloadProgressBlock)progressBlock
|
|
|
+ completed:(DownloadCompletedBlock)completedBlock
|
|
|
+{
|
|
|
+ self.CompletedBlock = completedBlock;
|
|
|
+ self.ProgressBlock = progressBlock;
|
|
|
+ self.downloadLocation = dlLocation;
|
|
|
+
|
|
|
+ NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"com.nito.installer"];
|
|
|
+
|
|
|
+ // Creating sessions
|
|
|
+ NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];
|
|
|
+
|
|
|
+ NSURLSession *backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfiguration delegate:self delegateQueue:operationQueue];
|
|
|
+ NSURLSessionDownloadTask *downloadTask = [backgroundSession downloadTaskWithURL:url];
|
|
|
+ [downloadTask resume];
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)URLSession:(NSURLSession *)session
|
|
|
+ downloadTask:(NSURLSessionDownloadTask *)downloadTask
|
|
|
+ didWriteData:(int64_t)bytesWritten
|
|
|
+ totalBytesWritten:(int64_t)totalBytesWritten
|
|
|
+totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
|
|
|
+{
|
|
|
+ //NSLog(@"Session %@ download task %@ wrote an additional %lld bytes (total %lld bytes) out of an expected %lld bytes.\n", session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
|
|
|
+ if (totalBytesExpectedToWrite != NSURLResponseUnknownLength) {
|
|
|
+
|
|
|
+ double percentComplete=(totalBytesWritten/(float)totalBytesExpectedToWrite)*100.0;
|
|
|
+ // NSLog(@"Percent complete - %f",percentComplete);
|
|
|
+
|
|
|
+ if((freq%updateFrequency) == 0){
|
|
|
+
|
|
|
+ if (self.ProgressBlock != nil)
|
|
|
+ {
|
|
|
+ self.ProgressBlock(percentComplete);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (self.FancyProgressBlock != nil)
|
|
|
+ {
|
|
|
+ NSString *mediaType = @"media";
|
|
|
+ NSString *pathExt = downloadLocation.pathExtension;
|
|
|
+ if ([pathExt isEqualToString:@"m4v"])
|
|
|
+ {
|
|
|
+ mediaType = @"video";
|
|
|
+ } else if ([pathExt isEqualToString:@"aac"])
|
|
|
+ {
|
|
|
+ mediaType = @"audio";
|
|
|
+ }
|
|
|
+ self.FancyProgressBlock(percentComplete, [NSString stringWithFormat:@"Downloading %@ file...", mediaType]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ freq++;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ NSLog(@"Bytes received - %lld",totalBytesWritten);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)URLSession:(NSURLSession *)session
|
|
|
+ downloadTask:(NSURLSessionDownloadTask *)downloadTask
|
|
|
+ didResumeAtOffset:(int64_t)fileOffset
|
|
|
+expectedTotalBytes:(int64_t)expectedTotalBytes
|
|
|
+{
|
|
|
+ NSLog(@"Session %@ download task %@ resumed at offset %lld bytes out of an expected %lld bytes.\n", session, downloadTask, fileOffset, expectedTotalBytes);
|
|
|
+}
|
|
|
+
|
|
|
+- (void)URLSession:(NSURLSession *)session
|
|
|
+ downloadTask:(NSURLSessionDownloadTask *)downloadTask
|
|
|
+didFinishDownloadingToURL:(NSURL *)location
|
|
|
+{
|
|
|
+ NSLog(@"Session %@ download task %@ finished downloading to URL %@\n", session, downloadTask, location);
|
|
|
+
|
|
|
+ // Perform the completion handler for the current session
|
|
|
+ //self.completionHandlers[session.configuration.identifier]();
|
|
|
+
|
|
|
+ // Open the downloaded file for reading
|
|
|
+ //NSError *readError = nil;
|
|
|
+ //NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:location error:&readError];
|
|
|
+ // ...
|
|
|
+
|
|
|
+ // Move the file to a new URL
|
|
|
+ NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
+ //NSURL *cacheDirectory = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] firstObject];
|
|
|
+ NSError *moveError = nil;
|
|
|
+ if ([fileManager fileExistsAtPath:self.downloadLocation])
|
|
|
+ {
|
|
|
+ [fileManager removeItemAtPath:self.downloadLocation error:nil];
|
|
|
+ }
|
|
|
+ if ([fileManager moveItemAtPath:location.path toPath:self.downloadLocation error:&moveError])
|
|
|
+ {
|
|
|
+ self.CompletedBlock(self.downloadLocation);
|
|
|
+ } else {
|
|
|
+ NSLog(@"error: %@", moveError);
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ if ([fileManager moveItemAtURL:location toURL:cacheDirectory error:&moveError]) {
|
|
|
+ // ...
|
|
|
+ }
|
|
|
+ */
|
|
|
+}
|
|
|
+
|
|
|
//deprecated / obsolete, SHOULD still work but should never be used.
|
|
|
- (void)downloadFileWithURL:(NSURL *)url
|
|
|
toLocation:(NSString *)dlLocation
|