2011-07-21 35 views
2

我的應用從互聯網下載視頻並將其保存在iPhone中。瞭解更新UIprogressView的下載進度

我使用這個代碼

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 

[request setHTTPMethod:@"GET"]; 
NSError *error; 
NSURLResponse *response; 


NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSString *videosFolderPath = [documentFolderPath stringByAppendingPathComponent:@"videos"]; 

//Check if the videos folder already exists, if not, create it!!! 
BOOL isDir; 
if (([fileManager fileExistsAtPath:videosFolderPath isDirectory:&isDir] && isDir) == FALSE) { 
    [[NSFileManager defaultManager] createDirectoryAtPath:videosFolderPath withIntermediateDirectories:YES attributes:nil error:nil]; 
} 

NSString *filePath = [videosFolderPath stringByAppendingPathComponent:@"name.mp4"]; 

if ([fileManager fileExistsAtPath:filePath ] == YES) { 
    NSLog (@"File exists"); 
} 
else { 
    NSLog (@"File not found"); 
    NSData *urlData; 
    NSString *downloadPath = @"http://www.mywebsite.com/name.mp4"; 
    [request setURL:[NSURL URLWithString:downloadPath]]; 
    urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    BOOL written = [urlData writeToFile:filePath atomically:NO]; 
    if (written) 
     NSLog(@"Saved to file: %@", filePath); 
    else {NSLog(@"problem");} 
} 

所有工作正常,但我想補充一個進度查看,指示下載的狀態。

的問題(我想,但我不知道)是我的NSURLConnection的還沒有自己的代表,所以像

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data 

-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{ 

方法不會被調用。

我試圖用

urlData = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

,但應用程序崩潰,當我嘗試寫文件

[urlData writeToFile:filePath atomically:NO]; 

我能做些什麼? 謝謝

回答

5

你正在做的是發送一個「同步」請求,這意味着正在下載HTTP響應的線程將掛起,直到所有數據都被獲取。即使您不想顯示下載進度的任何指標,在UI線程上執行此操作也永遠不會有好處。我建議使用NSURLConnection類,設置它的委託,並響應委託方法。一個小教程可以在http://snippets.aktagon.com/snippets/350-How-to-make-asynchronous-HTTP-requests-with-NSURLConnection找到。

一旦你連接的委託,可以在連接接收響應獲取內容長度:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response; 
    contentSize = [httpResponse expectedContentLength]; 
} 

然後,計算每次新數據到達下載的總進度,做這樣的事情:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    // download data is our global NSMutableData object that contains the 
    // fetched HTTP data. 
    [downloadData appendData:data]; 
    float progress = (float)[downloadData length]/(float)contentSize; 
    [progressView setValue:progress]; 
} 

UPDATE:

如何異步HTTP做基金會又如:http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

+0

好的,泰克!有用!但現在我有一個問題:使用我的舊方法,我可以使用「文檔文件夾」獲得視頻,這很容易...現在,我下載的視頻在哪裏?我怎麼知道路徑(也用於檢查,如果已經存在等...)?謝謝! – JAA

+0

對不起,我可以使用[nameFile writeToFile:Path自動:否]寫入,'fileExists'檢查是否可用!謝謝! – JAA

+0

你能否更新鏈接?它不可達。謝謝 – Vinh

1

您在此處使用同步請求。

該文檔說 - 一個同步負載建立在該類提供的異步加載代碼之上。調用線程在異步加載系統對專門爲此加載請求生成的線程執行URL加載時被阻塞。爲了執行同步加載,在調用線程中不需要特殊的線程或運行循環配置。

參考類參考here

您已經閱讀類的引用後,您可以發送異步請求或初始化請求,設置代理,並開始。