0

如何从服务器下载 Cocoa .bundle 文件,然后将其加载到应用程序中?我试过使用 zip,但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 解压缩并将其加载到应用程序中呢?

谢谢

4

2 回答 2

1

根据下载的文档:shouldDecodeSourceDataOfMIMEType:

如果下载的文件未编码,则不会调用此方法。

所以,我猜这可能与它有关。你可能最好实现 download:didReceiveResponse: 并检查 NSURLResponse 对象,尤其是状态码——如果不是 200 ,那就是出了问题,你需要查看 HTTP 代码以了解究竟是什么问题。

另外,我不确定这一点,您是否需要提升权限才能安装捆绑包,它是可执行文件?

于 2012-05-19T12:49:03.577 回答
1

shouldDecodeSourceDataOfMIMEType委托效果很好,但仅适用于gzip (.gz) 档案。我已经对 .zip 和 .gz 进行了广泛的测试。

还应该注意的是它不会调用 tar,所以如果你同时应用了压缩和 tar,如下所示:

tar czvf ArchiveName.tar.gz ./ArchiveName/

shouldDecodeSourceDataOfMIMEType委托会给你留下

ArchiveName.tar

因此,存档不会立即可用。

对于.zip 档案,正如其他人指出的那样,最好的选择是MiniZip (C API) 或基于它的 Objective-C 框架,如ZipArchive (2005) 或更新的SSZipArchive (2013)。

于 2014-06-05T14:57:57.977 回答