To serialize the pdf I believe you could use NSData dataWithURL:
and store in Core Data. I'm unsure as to how you'd deserialize back to a pdf however and view it with UIWebView.
Asked
Active
Viewed 887 times
0

Josh Elias
- 3,250
- 7
- 42
- 73
-
Another option is to host your PDF files on some web server and then download the PDF data from that server and save / cache it on the device. That would make downloading from the store (especially through cellular) and getting your user into your app faster. – Michael Dautermann Apr 04 '13 at 00:26
-
I realized that this was in fact two questions. The one you've provided an opinion on is here: http://stackoverflow.com/questions/15800589/loading-images-from-appbundle-vs-coredata. Also, what if my user doesn't enter the app until they're away from either wifi or cellular data? Is it good practice to display an error saying you must be online the first time you run this app? – Josh Elias Apr 04 '13 at 00:29
-
You're not serialising you're just storing binary data. You can then use the loaddata method to load the nsdata object, just set the correct mime type. – Bushbert Apr 04 '13 at 01:04
-
Put that in an answer. – Josh Elias Apr 04 '13 at 01:19
1 Answers
0
You can use CGPDFDocumentCreateWithProvider
:
If your NSData reference is pdfData
then:
CFDataRef myPDFData = (CFDataRef)pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
But I guess you just want to display it in a web view and the above is not what you need.
However if you already have the url wouldn't it be easier to store the URL rather than the pdf as such.
Also if you have the data you can write it into a file inside your documents directory temporarily and then use web view to load this url.
i.e;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"New1.pdf"];
NSURL *fileURL = [NSURLRequest requestWithURL:appFile];
[data writeToURL:fileURL atomically:YES];
and then :
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
And if you want to remove the file from the documents directory you could use:
[[NSFileManager defaultManager] removeItemAtURL:fileUrl error:nil];
to remove the temporary pdf you created.

Rakesh
- 3,370
- 2
- 23
- 41