Reputation: 1257
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:
My download code is as follows:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (!error) {
DDLogInfo(@"File downloaded to: %@", filePath);
}
}];
[downloadTask resume];
Upvotes: 0
Views: 383
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