iOS单独或在一个存档中下载每个文件

时间:2012-04-21 15:29:29

标签: ios download downloading

我正在开发一款iOS应用程序,我需要经常“推送”新级别的设备。 Level是一个HTML5页面,包含图像,javascript和css文件。我需要在用户播放级别之前在本地缓存所有级别的文件。每个级别约1.5MB。我正在考虑将整个级别的文件夹与HTML,图像,css和js存档到一个zip存档中,下载此zip文件然后在设备上取消存档。但也许单独下载每个文件更有效,因为它们可以同时下载?

这是我的第一个iOS应用程序,我不确定如何正确执行。所以问题是:从服务器异步下载整个文件夹然后在下载所有文件时触发回调的最佳方法是什么(请记住,用户可以在下载过程中关闭应用程序或丢失连接)? / p>

2 个答案:

答案 0 :(得分:1)

由于大多数用户实际上处于慢速3G连接状态,因此压缩存档下载速度会快得多(并且对他们来说更便宜),并且可能会完全满足他们的连接。同时执行此操作只会使所有传输速度变慢,因为它们被迫共享相同的有限连接。一次完成它也可以避免与许多传输文件传输失败相关的麻烦,例如。

答案 1 :(得分:0)

我发现压缩实际上比较慢,因为设备在下载时需要和解压缩一样长。如上所述,使用此查看ASIHTTPRequest,您可以说“获取页面及其所有资源” http://allseeing-i.com/ASIHTTPRequest/Setup-instructions

- (IBAction)downloadLevel:(NSURL *)levelURL
{
    // Assume request is a property of our controller
    // First, we'll cancel any in-progress page load
    [self.request setDelegate:nil];
    [self.request cancel];

    [self setRequest:[ASIWebPageRequest requestWithURL:levelURL]];
    [self.request setCompletionBlock:^{
        //code in here runs when the request finishes
    }];

    [self.request setFailedBlock:^{
        //code in here runs when the request fails
    }];

    // Tell the request to embed external resources directly in the page
    [self.request setUrlReplacementMode:ASIReplaceExternalResourcesWithData];

    // It is strongly recommended you use a download cache with ASIWebPageRequest
    // When using a cache, external resources are automatically stored in the cache
    // and can be pulled from the cache on subsequent page loads
    [self.request setDownloadCache:[ASIDownloadCache sharedCache]];

    // Ask the download cache for a place to store the cached data
    // This is the most efficient way for an ASIWebPageRequest to store a web page
    [self.request setDownloadDestinationPath:
     [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:self.request]];

    [self.request startAsynchronous];
}

此处可以找到更多文档 http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest
在这里 http://allseeing-i.com/ASIHTTPRequest/How-to-use
这一切都非常简单。