views:

570

answers:

3

I am able to successfully view a PDF from a website.

I want to be able to download that PDF to the device, then be able to access that file locally.

So the user opens the app, the app checks the online PDFs date and compares it to the date of the PDF store locally, if it is newer, then the new PDF is downloaded, if its not newer then it just 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?

A: 

You need to read the File and Data Management Guide from Apple. It will describe which locations are available in the application sandbox for saving files locally and how to get a reference to those locations. It also has a section for reading and writing :)

Enjoy!

willcodejavaforfood
A: 

I'd also recommend taking a look at ASIHTTPRequest for easy file downloading.

matkins
+3  A: 

I have found one method which i tried my self

//Get the PDF Data from the url in a NSData Object

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.msy.com.au/Parts/PARTS.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 ur PDF locally and also loads it into ur UIWebView.

Hope this solves ur problem.

RVN