Freek Sanders
Freek Sanders

Reputation: 1257

Get pdf download location from URL query

I have a PDF download location which is formatted as follows:

https://www.example.com/apps/user_name?txtFile=directory_1%2Fmy_pdf_file.pdf

Which NSURL would I need to pass to for example NSURLSessionDownloadTask in order to download this mentioned PDF (my_pdf_file.pdf)?

UPDATE: The problem seems to be related to the url redirection to a different location, which is unknown at the outset.

Further info:

Upvotes: 0

Views: 383

Answers (1)

Manuel Escrig
Manuel Escrig

Reputation: 2835

In order to download a PDF file using NSURLSession do the following:

NSString *londonWeatherUrl = @"https://www.example.com/apps/user_name?txtFile=directory_1%2Fmy_pdf_file.pdf";

First here is how you make this call when using NSURLConnection:

NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:londonWeatherUrl]];

[NSURLConnection sendAsynchronousRequest:request
   queue:[NSOperationQueue mainQueue]
   completionHandler:^(NSURLResponse *response,
                       NSData *data,
                       NSError *connectionError) {
      // handle response
}];

Now let’s use NSURLSession. Note that this is the simplest way to make a quick call using NSURLSession. Later in the tutorial you will see how to configure the session and setup other features like delegation.

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
          completionHandler:^(NSData *data,
                              NSURLResponse *response,
                              NSError *error) {
            // handle response

  }] resume];

Upvotes: 0

Related Questions