2013-05-06 26 views
0

我創建了一個應用程序,可以從任意大小的url下載文件。我在我的頁面中有一個按鈕和一個進度視圖。我想要點擊按鈕下載的文件並顯示下載狀態進度視圖。如何在xcode中使用ProgressView來顯示下載的狀態

我可以下載任何文件與此代碼,但我不知道如何使用發展觀與此代碼:

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

    }); 
} 

請指導我。

回答

2

在.h文件中定義的NSTimer *定時器並設置ProgressView的.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); 
     } 

    }); 
} 

,並添加這個方法

- (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]; 
    } 
} 
+0

我的朋友,我用故事板。這段代碼在xcode中是不同的? – emma 2013-05-06 08:45:57

+0

我的朋友,我在我的項目中添加此代碼,但我得到這個錯誤:'NSDate'沒有可見的@interface聲明選擇器'長度' – emma 2013-05-06 08:51:16

+0

更新我的答案。現在檢查,讓我知道:) – Ushan87 2013-05-06 09:06:44

相關問題