0

I am accessing the PDF files available in my PHP server using my iPhone with the help of passing file url to CGPDFDocumentCreateWithURL. My question whenever i access the files it will downloaded to my iphone documents directory or not?

Because if my application in active for some time i am getting the error CFURLCreateDataAndPropertiesFromResource with error code -14. After getting this error my application is not crashed but it is not functioning properly.

I am not able to understand the internal process? Please help me in this issue?

Vignesh Babu
  • 670
  • 13
  • 30

2 Answers2

2

I think you're misunderstanding what CGPDFDocumentCreateWithURL does. It does not download a PDF document to the filesystem. It creates a PDF in memory based off the URL provided. It doesn't save anything to the filesystem.

I would strongly recommend you use a networking class (NSURLConnection, ASI, etc) to download the PDF first and then open it. This will enable you to provide better feedback, and support more advanced download features such as resuming partial downloads, providing progress, and better thread management.

lxt
  • 31,146
  • 5
  • 78
  • 83
0

To save the PDF

NSString *urlString = @"http://www.web.com/url/for/document.pdf";

NSURL *url = [NSURL URLWithString:urlString];

NSData *data = [NSData dataWithContentsOfURL:url];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myLocalFileName.pdf"];

[data writeToFile:pdfPath atomically:YES];

then load that saved file into a web view:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myLocalFileName.pdf"];

NSURL *url = [NSURL fileURLWithPath: pdfPath];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[[self mainWebView] loadRequest:request];
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
Muhammed Sadiq.HS
  • 773
  • 1
  • 11
  • 35
  • This isn't a great way of doing it, or at the least you should be running it off the main thread. `dataWithContentsOfURL` is a synchronous method, which is ill advised for networking. – lxt Jan 18 '12 at 11:26