私のアプリはインターネットからビデオをダウンロードし、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];
を試してみましたか? ありがとう
Ok、thaks!できます!しかし、今私は1つの質問があります:私の古い方法を使用して、私は "ドキュメントフォルダ"を使用してビデオを得ることができました、それは簡単でした...今すぐダウンロードしたビデオはどこですか?どうすればパスを知ることができますか?ありがとう! – JAA
申し訳ありませんが、[nameFile writeToFile:自動的にパス:NO]を使用して書き込みできます。使用可能かどうかを確認する 'fileExists'もあります。ありがとう! – JAA
リンクを更新してください。それは到達可能ではありません。ありがとう – Vinh