download and save zip file to iphone

2019-09-08 03:54发布

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

1条回答
聊天终结者
2楼-- · 2019-09-08 04:40

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:.

查看更多
登录 后发表回答