0

I have to download a pdf from url on a button click in Objective-C

This is what I have done:

- (IBAction)DownLoadPdfButton:(UIButton *)sender {
    
   NSString *currentURL = @"http://africau.edu/images/default/sample.pdf";
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];

            NSURLConnection *conn = [[NSURLConnection alloc] init];
        (void)[conn initWithRequest:request delegate:self];

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
           NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:currentURL]];
            dispatch_sync(dispatch_get_main_queue(), ^{
            });
           NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

           NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
            [data writeToFile:filePath atomically:YES];
        });
    
}

I don't want to declare any path for saving my pdf.. please provide answer only for downloading pdf on button click and if pdf is not downloading then show error on console.

I have refered other answers.. but they are not useful.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Sarthak
  • 29
  • 1
  • 6
  • "I don't want to declare any path for saving my pdf" Why? What's happening with your code? Is `data` nil? `writeToFile:atomically:` returns a boolean value, is it true? You might want to use `URLSession` instead of `dataWithContentsOfURL`, because it could bring more infos in case of download fails. Also, you dont use `conn`? Which should be deprecated by the way... And you should use `NSSearchPathForDirectoriesInDomains`, not `[NSBundle main]`. – Larme Apr 26 '22 at 15:15
  • can you please provide answer using url session...and yes that conn is deprecated. – Sarthak Apr 26 '22 at 15:34
  • https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_from_websites?language=objc That's the second link I got on my SEO with "Objective-C Download file" – Larme Apr 26 '22 at 15:35
  • while refering to [this](https://stackoverflow.com/a/36705589/18362158) solution I am getting error : "Use of undeclared identifier 'NSURLDownload'"....please provide solution – Sarthak Apr 26 '22 at 15:48
  • @Larme document you have shared is in swift...I need objc code for downloading PDF from url. – Sarthak Apr 26 '22 at 18:45
  • @Sarthak have a look here : https://stackoverflow.com/questions/16392420/how-to-download-files-from-url-and-store-in-document-folder – Shabnam Siddiqui Apr 27 '22 at 04:00
  • @ShabnamSiddiqui it is working .. i simply want to download the pdf ..I dont want to add path to save the pdf or that file name. – Sarthak Apr 27 '22 at 04:51
  • @ShabnamSiddiqui where i can see my downloaded pdf ? – Sarthak Apr 27 '22 at 05:02

0 Answers0