Objective c 从服务器下载cocoa包

Objective c 从服务器下载cocoa包,objective-c,cocoa,lazy-loading,Objective C,Cocoa,Lazy Loading,如何从服务器下载Cocoa.bundle文件,然后将其加载到应用程序中?我尝试过使用zip,但是没有调用shouldDecodeSourceDataOfMitype函数 - (IBAction)testDownload:(id)sender { // Create the request. NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/

如何从服务器下载Cocoa.bundle文件,然后将其加载到应用程序中?我尝试过使用zip,但是没有调用
shouldDecodeSourceDataOfMitype
函数

- (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解压并加载到应用程序中呢


谢谢

根据下载文档:shouldDecodeSourceDataOfMitype:

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

所以,我猜这可能与此有关。您最好实现download:didReceiveResponse:并检查NSURLResponse对象,尤其是状态代码——如果不是200,则表示出现了问题,您需要查看HTTP代码以了解问题的具体原因


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

shouldDecodeSourceDataOfMitype委托非常有效,但仅适用于gzip(.gz)归档文件。我已经用.zip和.gz进行了广泛的测试

还应注意,它不调用tar,因此,如果您同时应用压缩和tar,如中所示:

tar czvf ArchiveName.tar.gz ./ArchiveName/
shouldDecodeSourceDataOfMitype代表将为您留下:

ArchiveName.tar
因此,该档案将无法立即使用


对于.zip存档,正如其他人指出的,您最好的选择是(C API)或基于它的Objective-C框架,如(2005)或最近的SSZipArchive(2013)。

好吧,看起来我搞错了,响应没有编码,但是内容类型是application/zip,所以NSURLDownload无法对其进行解码,因为它只对编码响应进行解码,而不对编码文件进行解码,所以我应该以什么方式对下载的.zip进行编码?如果它是压缩的,您应该解压缩它。Apple文档告诉您通过命令行(Shell Scripting Primer>>“归档和压缩命令”)执行此操作。还有各种库可以为您压缩或解压,它们的优缺点将在StackOverflow上讨论。到目前为止,我从未使用过它们,所以在这方面我没有任何建议。