Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display pdf on iOS

Tags:

ios

pdf

uiwebview

I want to know if there is any way for a offline build for xcode iOS such that we can display pdf file from local file.

The method I'm using now is via UIWebView to display a custom HTML which contain the PDF URL (online) , but I want to make an offline version if possible.

But from online research, it seems to me the only way to display PDF is via UIWebView, if that is really the case, is it possible to pull the file from local resource (e.g. add files to my xcode resource folder) rather than using the PDF URL.

OFF-TOPIC: Which would be a better choice? To pull local file or to use the online URL for PDF file?

As UIWebView require connection, it would seem to be a better choice to just pull the online URL as it will be the most current and updated.

Feedback is much appreciated.

EDIT: quick question, if I use the UIDocumentInteractionController method or the QuickLook framework to make it offline, this would mean I have to release update patch each time there is new PDF article added in (correct me if I'm wrong)

Taking this point into consideration, I've come up with a counter-argument to my own question on the spot (Sorry if any of you feel you are wasting time on me >.<, I am a person who like to question possibilities and the different approach one could take to solve a problem) In the event to save time from the constant updating and providing a real time base solution, which is to use UIWebView (I know this is contradicting my purpose of this SO thread), as this proj is base on a online website (getting my content and sources from there), if I were to load their website for the article section, would it be bold of me to say that by adding a NSString variable, I can add in the NSRange function to the viewDidLoad together with the UIWebView such that the URL will never leave the news section and PDF URL. e.g. using combination of if and NSRange function to counter check that the URL only contain the news section and PDF URL)

I did once before a NSRange function for point on calculator so I thought it would be a good time to put it in use for this framework app.

P.S - This app is a organization base project and I'm a intern providing them with the base skeleton framework.

like image 304
Yang Jie Domodomo Avatar asked Jun 04 '12 09:06

Yang Jie Domodomo


People also ask

Can I put a PDF on my iPhone Home Screen?

Upload the file, then browse to the file with Safari, then click the "right arrow in a box" icon at the bottom of the screen. From there, you have nine options for the file, one of which is "add to Home Screen".

Does iOS have a PDF viewer?

View PDFs across devices You can see PDFs and books that are not from the Book Store across your iPhone, iPad, iPod touch, and Mac where you're signed in with the same Apple ID.

Why can I not view a PDF on my iPhone?

If you're trying to open a PDF on an iPad or iPhone and it appears blank, you need to set Adobe Reader as your default for opening PDF files on your device. 💡Tip: Select the Preview icon to quickly preview PDFs without downloading them.


2 Answers

Many options, here are 3:

1) The easiest way to load and display a local pdf file is to use a UIWebview like that:

NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; 

2) You can also use a UIDocumentInteractionController/QLPreviewController to display PDF Files natively.

3) Another way would be to build a custom PDF Viewer, as in apples ZoomingPDFViewer example code. (using UIPageViewController + CATiledLayer + UIScrollView)

like image 68
calimarkus Avatar answered Sep 19 '22 11:09

calimarkus


You can use a UIWebView to get the PDF from your bundle directly, no need to call an online web service.

Local File:

NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil]; [self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]]; 

Remote File:

- (void) loadRemotePdf     {        CGRect rect = [[UIScreen mainScreen] bounds];        CGSize screenSize = rect.size;                 UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];         webView.autoresizesSubviews = YES;             webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);         NSURL *myUrl = [NSURL URLWithString:@"http://www.mysite.com/test.pdf"];        NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];                     [webView loadRequest:myRequest];         [window addSubview: myWebView];        [myWebView release];              } 
like image 42
self Avatar answered Sep 19 '22 11:09

self