我目前正在開發使用異步下載的iPad iOS 6應用程序。 要接收我使用代表NSURLConnectionDownloadDelegate
的進度信息。 下載和收到的進度NSURLConnectionDownloadDelegate destinationURL
– connection:didWriteData:totalBytesWritten:expectedTotalBytes:
工作得很好。
然而,一旦下載完成後,我不知道如何將數據提取出來的NSURL
destinationURL
由delegatemethod
– connectionDidFinishDownloading:destinationURL:
的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
當使用方法時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);
}];
如果你將文件複製到你的內容的路徑,並嘗試有開嗎? – jimpic
試試這個答案:http://stackoverflow.com/questions/12582849/nsurlconnectiondownloaddelegate-expectedtotalbytes-zero-in-ios-6 – emrys57
你看了以前的評論答案? – emrys57