Is it possible to open a PDF from a website in Safari in order to save it to local disk?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the NSURL class to download the pdf file to your documents directory, bypassing the need to open it in Safari (and subsequently terminating your own app).
UIWebView makes it nice and easy to display external PDFs as well as local files (just point the correct filepath at it), so you could even download the PDF to your documents folder and then display it from the local cache at a later date.
Added some sample code below
For a simpler example, you might find this is acceptable for your app; This will download the file to your documents folder but uses a blocking function (
initWithContentsOfURL), so you may run into problems with large files/slow connections:(This code should be all you need, but you will probably want to create a function to handle this step/handle memory/check for errors etc)
To give you a basic sample to build from, the following code is enough to display a PDF file inside from your Documents folder in a webview:
Should you want to display the PDF directly from the website (rather than a local file) then you can have
pdfPathcontain the full URL to the file instead.