如何从服务器下载Cocoa .bundle文件并将其加载到应用程序中?我尝试过使用压缩包,但是没有调用shouldDecodeSourceDataOfMIMEType函数。
- (IBAction)testDownload:(id)sender {
// Create the request.
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/SampleBundle.zip"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// Create the connection with the request and start loading the data.
NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest
delegate:self];
if (theDownload) {
// Set the destination file.
[theDownload setDestination:@"/Users/developer/Desktop/Test-Downloads/SampleBundle" allowOverwrite:YES];
} else {
// inform the user that the download failed.
}
}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
download = nil;
// Inform the user.
NSLog(@"Download failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)downloadDidFinish:(NSURLDownload *)download
{
download = nil;
// Do something with the data.
NSLog(@"%@",@"downloadDidFinish");
}
- (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType;
{
BOOL shouldDecode = YES;
NSLog(@"EncodingType: %@",encodingType);
return shouldDecode;
}那么,如何从服务器下载.bundle,将其解压缩并加载到应用程序中呢?
谢谢
发布于 2012-05-19 20:49:04
根据download:shouldDecodeSourceDataOfMIMEType的文档:
如果下载的文件未编码,则不调用此方法。
所以,我猜这可能和它有关。您最好实现download:didReceiveResponse:并检查NSURLResponse对象,特别是状态码--如果不是200,那么就是出了问题,您需要查看HTTP码,看看到底是什么问题。
另外,我不确定这一点,你需要提升权限才能安装一个包,它是一个可执行的包吗?
发布于 2014-06-05 22:57:58
shouldDecodeSourceDataOfMIMEType代表的效果很好,但只能在gzip (.gz)归档文件上使用。我已经用.zip和.gz进行了广泛的测试。
还应该注意的是,它不调用tar,所以如果您同时应用了压缩和tar,就像下面的代码一样:
tar czvf ArchiveName.tar.gz ./ArchiveName/shouldDecodeSourceDataOfMIMEType代表将为您提供以下内容:
ArchiveName.tar因此,归档文件不会立即可用。
对于.zip archives,正如其他人指出的那样,您最好的选择是 (C API)或基于它的Objective-C框架,如 (2005)或更新的SSZipArchive (2013)。
https://stackoverflow.com/questions/10665206
复制相似问题