简体   繁体   中英

Download a cocoa bundle from server

how can I download a Cocoa .bundle file from a server and then load it into a app? I've tried using a zip but the shouldDecodeSourceDataOfMIMEType function doesn't get called.

- (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;
}

So how can I download a .bundle from a server uncompress it and load it into the application?

Thanks

According to the documentation of download:shouldDecodeSourceDataOfMIMEType:

This method is not called if the downloaded file is not encoded.

So, I'm guessing that might have something to do with it. You're probably better off implementing download:didReceiveResponse: and examining the NSURLResponse object, especially the status code -- if that's not 200 than something is going wrong and you need to look at HTTP codes to see what exactly the problem is.

Also, I'm not sure of this, do you need elevated permissions to install a bundle, it being an executable?

The shouldDecodeSourceDataOfMIMEType delegate works great but only on gzip (.gz) archives . I've tested extensively with both .zip & .gz.

It should also be noted that it doesn't call tar, so if you applied compression and tar at the same time as in:

tar czvf ArchiveName.tar.gz ./ArchiveName/

the shouldDecodeSourceDataOfMIMEType delegate will leave you with:

ArchiveName.tar

So, the archive will not be immediately useable.

For .zip archives , as others have pointed out, your best bet is MiniZip (C API) or a Objective-C framework based on it like ZipArchive (2005) or the more recent SSZipArchive (2013).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM