下载 zip 文件并将其保存到 iPhone

发布于 2024-10-07 03:36:40 字数 112 浏览 7 评论 0原文

我想从网络下载 zip 文件,但无法弄清楚我如何

可以下载图像/文本/xml 文件,但无法下载 zip 文件

有人可以指导我如何从网络下载 zip 文件吗?

谢谢

i want to download the zip file from web but unable to figure out that how it is possible

i can download image /text/xml file but unable to download a zip file

Can someone guide me how to download zip files from web?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-10-14 03:36:40

如果您使用 < code>NSURLConnection,无论文件的类型如何,它的工作方式都完全相同。


示例:(凭空想象,不能保证它能以这种方式工作,显然您应该实现错误检查)

- (void) download
{
    self.loadedData = [NSMutableData data];         // make 'loadedData' a property of the class

    NSURL *url = [NSURL URLWithString:@"http://..."];
    NSMutableURLRequest  *urlRequest = [NSMutableURLRequest requestWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                           timeoutInterval:20.0];
    [urlRequest setValue:@"Optional User Agent" forHTTPHeaderField:@"User-Agent"];

    // shoot it off
    NSURLConnection *mainConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
    if (nil == mainConnection) {
        NSLog(@"Could not create the NSURLConnection object");
    }
}

然后您必须在委托方法中处理传入的数据,例如仅保存您的数据:

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [loadedData appendData:data];
}

查看其他委托方法并实现它们,您应该处理身份验证挑战和失败响应。例如,您还可以

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

connection:didReceiveResponse: 中设置:并在 connectionDidFinishLoading: 中再次将其设置为 NO

If you're using NSURLConnection, it works exactly the same way no matter which type the file has.


Example: (typed off of my head, no guarantee that it works this way and you should obviously implement error checking)

- (void) download
{
    self.loadedData = [NSMutableData data];         // make 'loadedData' a property of the class

    NSURL *url = [NSURL URLWithString:@"http://..."];
    NSMutableURLRequest  *urlRequest = [NSMutableURLRequest requestWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                           timeoutInterval:20.0];
    [urlRequest setValue:@"Optional User Agent" forHTTPHeaderField:@"User-Agent"];

    // shoot it off
    NSURLConnection *mainConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
    if (nil == mainConnection) {
        NSLog(@"Could not create the NSURLConnection object");
    }
}

Then you must handle the incoming data in the delegate methods, e.g. to just save your data:

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [loadedData appendData:data];
}

Take a look at the other delegate methods and implement them, you should deal with authentification challenges and fail responses. You can also for example set:

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

in connection:didReceiveResponse: and set it to NO again in connectionDidFinishLoading:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文