and*_*dre 6 nsurlconnection nsdata ios nsurlconnectiondelegate ios6
我目前正在开发使用异步下载的iPad iOS 6应用程序.为了接收进度信息,我使用了代表NSURLConnectionDownloadDelegate.下载和收到的进度
– connection:didWriteData:totalBytesWritten:expectedTotalBytes:
工作得很好.
但是一旦下载完成,我不知道如何NSURL destinationURL从delegate方法提供的数据中提取数据
– 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);
}];
检查文件是否存在。我有同样的问题,我必须使用 NSURLConnectionDataDelegate 并手动保存数据,这就是原因: NSURLConnectionDownloadDelegate 存在与某些文件扩展名相关的错误。
您还可以在这里看到同一问题的答案: NSURLConnectionDownloadDelegate file issues
| 归档时间: | 
 | 
| 查看次数: | 1994 次 | 
| 最近记录: |