I am downloading video from URL to the documents directory. There is no constant size for videos. How can I show a download status for the users. Is there any way to show the remaining bytes to be downloaded ?
Here is my code -
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *urlToDownload = @"http://someurl.mp4";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp4", documentsDirectory,[[[array objectAtIndex:i] objectForKey:@"name"]stringByReplacingOccurrencesOfString:@" " withString:@"_"]];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved -------- %@!",filePath);
});
}
}
});