Ios NSURLConnectionDownloadDelegate destinationURL
我目前正在开发一款使用异步下载的iPadiOS6应用程序。 为了接收进度信息,我使用了委托Ios NSURLConnectionDownloadDelegate destinationURL,ios,ios6,nsurlconnection,nsdata,nsurlconnectiondelegate,Ios,Ios6,Nsurlconnection,Nsdata,Nsurlconnectiondelegate,我目前正在开发一款使用异步下载的iPadiOS6应用程序。 为了接收进度信息,我使用了委托NSURLConnectionDownloadDelegate。 下载和收到的进度 – connection:didWriteData:totalBytesWritten:expectedTotalBytes: 很好用 但是,下载完成后,我不知道如何从delegatemethod提供的NSURLdestinationURL中提取数据 – connectionDidFinishDownloading:des
NSURLConnectionDownloadDelegate
。
下载和收到的进度
– connection:didWriteData:totalBytesWritten:expectedTotalBytes:
很好用
但是,下载完成后,我不知道如何从delegatemethod提供的NSURL
destinationURL
中提取数据
– 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..
}
编辑2: 当使用
NSURLConnection
的sendsynchronousrequest
方法时,我可以提取NSData
。然而像这样,我无法确定下载的进度
[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);
}];
/private/var/mobile/Applications/7CB3B194-9E79-4F0B-ACFD-7B87AA8C7BAF/tmp/filename.mp4
是路径,而不是URL。请改用initWithContentsOfFile:尝试初始化。检查该文件是否存在。
我也有同样的问题,我必须使用NSURLConnectionDataDelegate手动保存数据,这就是为什么:
与某些文件扩展名相关的NSURLConnectionDownloadDelegate出现错误
你也可以在这里看到相同问题的答案:
如果您将文件复制到内容路径并尝试在那里打开该文件会怎么样?尝试以下回答:您查看了上一条评论中的答案吗?是的,但是delegateMethod连接:didWriteData:TotalBytesWrite:expectedTotalBytes:工作正常,我可以使用进度条显示下载进度。因此,我认为问题不在头文件中。是的,NSURLConnectionDownloadDelegate可能存在问题。我将委托替换为NSConnectionDataDelegate,现在我可以异步下载一个文件,并且仍然显示下载的进度。