Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download PDF and store it locally on iPhone?

I am able to successfully view a PDF from a website. I want to be able to download that PDF to the device, then access that file locally.

When the app is opened, it will check the online PDF's date. If it is newer than the locally-stored PDF, the app will download the new one, otherwise it opens the locally-stored PDF.

The code I am currently using:

PDFAddress = [NSURL URLWithString:@"http://www.msy.com.au/Parts/PARTS.pdf"]; request = [NSURLRequest requestWithURL:PDFAddress]; [webView loadRequest:request]; webView.scalesPageToFit = YES; 

How am I able to achieve this?

like image 550
Zac Altman Avatar asked Feb 09 '10 03:02

Zac Altman


People also ask

How do I save a PDF locally?

To save a PDF, choose File > Save or click the Save File icon in the Heads Up Display (HUD) toolbar at the bottom of the PDF. The Save As dialog box is displayed. Choose the location where you want to save the PDF and then click Save.

How do I store files locally on my iPhone?

Tap Share, then scroll down and select Save to Files. Now choose a folder under On my iPhone and then tap Save in the top right. To move a document from your iCloud to your iPhone, open up the files app and deep (or long) press the file that you want to move until some options come up.


2 Answers

I have found one method which I tried myself:

// Get the PDF Data from the url in a NSData Object NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[     NSURL URLWithString:@"http://www.example.com/info.pdf"]];  // Store the Data locally as PDF File NSString *resourceDocPath = [[NSString alloc] initWithString:[     [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]         stringByAppendingPathComponent:@"Documents" ]];  NSString *filePath = [resourceDocPath      stringByAppendingPathComponent:@"myPDF.pdf"]; [pdfData writeToFile:filePath atomically:YES];   // Now create Request for the file that was saved in your documents folder NSURL *url = [NSURL fileURLWithPath:filePath]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];  [webView setUserInteractionEnabled:YES]; [webView setDelegate:self]; [webView loadRequest:requestObj]; 

This will store your PDF locally and load it into your UIWebView.

like image 99
RVN Avatar answered Oct 02 '22 04:10

RVN


In Swift 4.1

// Url in String format let urlStr = "http://www.msy.com.au/Parts/PARTS.pdf"  // Converting string to URL Object let url = URL(string: urlStr)  // Get the PDF Data form the Url in a Data Object let pdfData = try? Data.init(contentsOf: url!)  // Get the Document Directory path of the Application let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL  // Split the url into a string Array by separator "/" to get the pdf name let pdfNameFromUrlArr = urlStr.components(separatedBy: "/")  // Appending the Document Directory path with the pdf name let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrlArr[     pdfNameFromUrlArr.count - 1])  // Writing the PDF file data to the Document Directory Path do {     _ = try pdfData.write(to: actualPath, options: .atomic)  }catch{      print("Pdf can't be saved") }  // Showing the pdf file name in a label lblPdfName.text = pdfNameFromUrlArr[pdfNameFromUrlArr.count - 1]  // URLRequest for the PDF file saved in the Document Directory folder let urlRequest = URLRequest(url: actualPath)  webVw.isUserInteractionEnabled = true webVw.delegate = self webVw.loadRequest(urlRequest) 

If you want to save the Pdf's in a particular folder/directory inside Main Document Directory

let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL  // The New Directory/folder name let newPath = resourceDocPath.appendingPathComponent("QMSDocuments")  // Creating the New Directory inside Documents Directory do {     try FileManager.default.createDirectory(atPath: newPath.path, withIntermediateDirectories: true, attributes: nil) } catch let error as NSError {     NSLog("Unable to create directory \(error.debugDescription)") }  // Split the url into a string Array by separator "/" to get the pdf name pdfNameFromUrlArr = urlStr.components(separatedBy: "/")  // Appending to the newly created directory path with the pdf name actualPath = newPath.appendingPathComponent(pdfNameFromUrlArr[pdfNameFromUrlArr.count - 1]) 

Happy Coding :)

like image 20
Ariven Nadar Avatar answered Oct 02 '22 03:10

Ariven Nadar