0

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);

            });
             }
             }
          });
rmaddy
  • 307,833
  • 40
  • 508
  • 550
Nithin M Keloth
  • 1,585
  • 1
  • 20
  • 36

1 Answers1

0

Use NSURLConnection instead: http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

And use its delegate functions to get progress: https://stackoverflow.com/a/2269670/78496

Community
  • 1
  • 1
chedabob
  • 5,757
  • 2
  • 25
  • 44