2011-07-21 3 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");} 
} 

すべてが正常に動作します。このコードを使用していますが、私はダウンロードのステータスを示すために、進行状況の表示を追加したいです。

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

または

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

のようなメソッドが呼び出されることはありませんので、

問題は(私は思うが、私はわからないんだけど)、私のNSURLConnectionはデリゲートとしての地位をしていないということです。

私はファイルの書き込みしようとすると、私は何ができる

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

Ok、thaks!できます!しかし、今私は1つの質問があります:私の古い方法を使用して、私は "ドキュメントフォルダ"を使用してビデオを得ることができました、それは簡単でした...今すぐダウンロードしたビデオはどこですか?どうすればパスを知ることができますか?ありがとう! – JAA

+0

申し訳ありませんが、[nameFile writeToFile:自動的にパス:NO]を使用して書き込みできます。使用可能かどうかを確認する 'fileExists'もあります。ありがとう! – JAA

+0

リンクを更新してください。それは到達可能ではありません。ありがとう – Vinh

1

ここで同期要求を使用しています。

ドキュメントには、 - と書かれています。同期ロードは、クラスで使用可能な非同期ロードコードの上に構築されています。非同期ロードシステムがこのロード要求用に特別に生成されたスレッドにURLロードを実行している間、呼び出し側スレッドはブロックされます。同期ロードを実行するために、呼び出し側スレッドに特別なスレッドまたは実行ループ構成は必要ありません。

を使用すると、クラス参照を読んだ後、あなたは非同期要求を送信したり、要求を初期化し、デリゲートを設定し、開始することができますいずれかのクラス参照にhere

を参照してください。

関連する問題