現在、非同期ダウンロードを使用する iPad iOS 6 アプリケーションを開発しています。進捗情報を受け取るために、デリゲートを使用しましたNSURLConnectionDownloadDelegate
。によって受信されたダウンロードと進行状況
– connection:didWriteData:totalBytesWritten:expectedTotalBytes:
うまく動作します。
ただし、ダウンロードが完了するとNSURL
destinationURL
、delegatemethod によって提供されたデータを抽出する方法がわかりません
– connectionDidFinishDownloading:destinationURL:
String で受け取った destinationURL は " /private/var/mobile/Applications/7CB3B194-9E79-4F0B-ACFD-7B87AA8C7BAF/tmp/filename.mp4
"のようになります
次に、指定された NSURL からデータを抽出しようとします。
NSData *data = [[NSData alloc] initWithContentsOfURL:destinationURL];
ただし、データは空です...
NSLog(@"data Size: %@", data.length); //prints out 0
を使用して誰かが同じ問題を抱えていNSURLConnectionDownloadDelegate
ますか? 解決方法はありますか?
編集:最初にファイルをコピーしてから、[NSData initWithContentsOFFile]
提案どおりに使用しようとしましたが、データ サイズはまだ 0 です。
- (void) connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
{
[[NSFileManager defaultManager] copyItemAtPath:destinationURL.path toPath:DEST_PATH error:nil];
NSData *data = [[NSData alloc] initWithContentsOfFile:DEST_PATH];
NSLog(@"data Size: %i", data.length); //still returns 0..
}
Edit2: のメソッドをNSData
使用すると、 を抽出できます。しかし、このように、ダウンロードの進行状況を判断できません...sendAsynchronousRequest
NSURLConnection
[NSURLConnection sendAsynchronousRequest:theRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (data){
NSLog(@"Size of the data: %i Bytes(s)", data.length); //works fine
[data writeToFile:DEST_PATH atomically:YES];
//From this point i can use the file at DEST_PATH
NSLog(@"Succeeded!");
}
else if (error)
NSLog(@"%@",error);
}];