0

I create one application that download file from url with any size.I have one button and one progress view in my page . I want when click on button downloaded file and show me download's status in progress view.

I can download any file with this code but I dont know how to use progress view with this code:

- (IBAction)Download:(id)sender
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Downloading Started");
        NSString *urlToDownload = @"http://192.168.1.100/emma/Adele%20-%20Someone%20Like%20You%20(MTV%20Video%20Music%20Awards%202011)%20HD%20Live.mkv";
        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:@"%@/%@", documentsDirectory,@"filename.mkv"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [urlData writeToFile:filePath atomically:YES];
                NSLog(@"File Saved !");
            });
            x = 1;
            NSLog(@"x : %d",x);
        }

    });
}

please guide me about it.

emma
  • 117
  • 1
  • 6
  • 17

1 Answers1

2

define NSTimer *timer in your .h file and set your ProgressView in your .xib

- (IBAction)Download:(id)sender
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Downloading Started");
        NSString *urlToDownload = @"http://192.168.1.100/emma/Adele%20-%20Someone%20Like%20You%20(MTV%20Video%20Music%20Awards%202011)%20HD%20Live.mkv";
        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:@"%@/%@", documentsDirectory,@"filename.mkv"];

            timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(updateProgressView) userInfo:nil repeats:YES];
            [timer fire];


            dispatch_async(dispatch_get_main_queue(), ^{
                [urlData writeToFile:filePath atomically:YES];
                NSLog(@"File Saved !");
            });
            x = 1;
            NSLog(@"x : %d",x);
        }

    });
}

and add this method

- (void) updateProgressView{
    NSString *urlToDownload = @"http://192.168.1.100/emma/Adele%20-%20Someone%20Like%20You%20(MTV%20Video%20Music%20Awards%202011)%20HD%20Live.mkv";
    NSURL *url = [NSURL URLWithString:urlToDownload];
    NSData *urlData = [NSData dataWithContentsOfURL:url];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.mkv"];
    NSData *writtenData = [NSData dataWithContentsOfFile:filePath];

    float progress = [writtenData length]/(float)[urlData length];
    [progressView setProgress:progress];
    if (progress == 1.0){
         [timer invalidate];
    }
}
Ushan87
  • 1,608
  • 8
  • 15
  • my friend I add this code in my project but I get this error : No visible @interface for 'NSDate' declares the selector 'length' – emma May 06 '13 at 08:51
  • I'm calling that method in the Timer. Check my code. use it and let me know whether it's working – Ushan87 May 06 '13 at 09:29
  • my friend Im sorry. progressView what is it? I define it @property (strong, nonatomic) IBOutlet UIProgressView *progressView; right?? – emma May 06 '13 at 09:35
  • my friend dosen't work :( this is my code can you check and tell me my mistake please ? http://www.mediafire.com/download.php?mi41fz3fqveyd8u Im wait. – emma May 06 '13 at 09:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29472/discussion-between-ushan87-and-emma) – Ushan87 May 06 '13 at 09:53