Vishal
Vishal

Reputation: 178

Download using NSURLConnection

I have a UIImageView that is displaying an image using a URL, I have a download button that brings up a UIActionSheet which will then download the file using the URL i want to know how to do the coding for the download part of it using NSURLConnection.

- (IBAction)DownloadClick:(id)sender {

    UIActionSheet *Action = [[UIActionSheet alloc]
                         initWithTitle:@"Options" 
                         delegate:self 
                         cancelButtonTitle:@"Cancel" 
                         destructiveButtonTitle:nil
                         otherButtonTitles:@"Download", nil];

    [Action showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0) {  //The Download code will go here This is where i need ur help


          }

The Url is saved in a string named "Url".

Upvotes: 0

Views: 1528

Answers (3)

x4h1d
x4h1d

Reputation: 6092

I don't think you tried Google for this. Anyway, here is your code and don't forget to add NSURLConnectionDataDelegate,

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {  //The Download code will go here This is where i need ur help

    //-------------------------------------------------------
    NSURL *url = [NSURL URLWithString:@"your_data_url"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:60.0];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (connection) {
        NSLog(@"succeed...");
    } else {
        NSLog(@"Failed...");
    }
    //-------------------------------------------------------------------
  }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

}

Upvotes: 1

Vinayak Kini
Vinayak Kini

Reputation: 2919

Make use of asynchronous request for image download. It can also help you identify errors, if any during image downloading.

 NSURL* url = [NSURL URLWithString:@"http://imageAddress.com"];
 NSURLRequest* request = [NSURLRequest requestWithURL:url];


[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
    NSData * data,
    NSError * error) {
if (!error){
            UIImage* image = [[UIImage alloc] initWithData:data];
           // do whatever you want with image
           }

}];

Upvotes: 1

Marco Pace
Marco Pace

Reputation: 3870

If you need synchronous download you can try this code:

NSURL *imageUrl = [NSURL URLWithString:@"http://site.com/imageName.ext"];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [[UIImage alloc] initWithData:imageData];

Now you have an image that you can use.

If you need an asynchronous request I suggest you to read the NSURLConnectionDataDelegate, that helps you handle the download of the file.

Upvotes: 0

Related Questions