Open PDF file contained in the URL - iphone

I have a link that contains a downloadable pdf file. This link doesn't have its extension as .pdf, its just a normal link. Its a web-service where i pass "PDF" as a parameter that returns me a PDF file. Whenever I open such link on a desktop browser, it directly ask for downloading the pdf. Into my ipad app, i need to display such pdf in a web view. What can be the possible solution?

You should make a url request and load that in to a Webview like this,
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
The url is your link to the PDF file and webview is UIWebView object where you can view the PDF.

Related

HTML file creation with image

I created an HTML file programmatically with images sources. The images are stored in the documents directory. The problem is that when I give the image source path as document directory path the images are not shown in Windows.
So, I choose the second option is to give only the image's name instead of whole path. Then it work well on Mac & Windows, but now images are not shown on the device's UIWebView.
Then I selected to open the HTML file in Safari insted of UIWebView, but the safari could not be opened because URL becomes NULL.
My code to open Safari is as follow:
NSLog(#"%#",documentsDirectory);
NSString *urlStr = [NSString stringWithFormat:#"%#/AccReport1.html",documentsDirectory];
NSLog(#"urlStr :: %#",urlStr);
NSURL *url1=[[NSURL alloc]init ];
url1 = [NSURL fileURLWithPath:urlStr];
NSLog(#"url :: %#",url1);
[[UIApplication sharedApplication]openURL:url1];
I want to mail the HTML file that contains images. And these HTML file and images are stored in document directory. I want to show this HTML file with images in windows browser, mac browser and iphone's safari.
How to do this?
One of the simple way to solve your problem is to keep Images and HTML file in same directory and refer those images in HTML files just by name and if you put them in a folder then give the complete path.
The images are not showing because the image path specified within the HTML code is not the documents directory path.
I suggest you might have to construct an html string and then subject the uiwebview to load that string
NSString *htmlString = [NSString stringWithFormat:#"<html><body><img src=\" %#/image.png \"></img></body></html>",documentsDirectoryPath];
[webView loadHTMLString:htmlString baseURL:nil];

how to access the content within the zip file in iphone

I have a zip file. In that zip file I have some ppt presentation.
I want to show that ppt presentation in my UIWebView.
I cannot extract and show ppt files there directly .
How do I access the ppt inside the zip in objective c?
This isnt easy. You might want to consider doing this another way. For example you could put the file unzipped on a server and open the link in a webview.
However, if you really want to do it this way there is 2 step.
1. Unzip the file.
You want end up with NSData. Read though some of the links suggested in the comments. You will need to use a 3rd party library to achieve this.
2. Load the data in to a UIWebView.
Write the NSData to the temp directory then point the UIWebView at it.
NSString *path = // .. Get a location in the NSTemporaryDirectory
if ([pptData writeToFile:path atomically:YES])
{
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}

UIWebView loadData and iWork Files

I am attempting to open an iWork 10 Numbers file placed on a Web site into a UIWebView. I tried with the following, and what happens is that the UIWebView loads blank. If I download the file to local the Documents folder and then load it in the UIWebView it then works. I'd like to read an iWork file without downloading them...
FYI, I have no issues with PDF, PPT, DOC, XLS...
Can anyone tell me how to load an iWork file from NSdata to UIWebView?
[webView loadData:receivedData
MIMEType:file.mediaMimetype
textEncodingName:nil
baseURL:[NSURL URLWithString:PATHTOFILE]
];
The tested values for mediaMimetype are:
application/vnd.apple.numbers
application/x-iwork-numbers-sffnumbers
You need to compress iWork file with .zip extension to display in webview.
refer this link for detail:
http://developer.apple.com/library/ios/#qa/qa1630/_index.html

How to make hyperlinks in pdf displayed in UIWebView work

Given a pdf created with pages that includes working imbedded hyperlink, stored as a resource with my app and displayed in a UIWebView, what needs to be done to make the hyperlink work in in the UIWebView.
Scouring the posts here and elsewhere on this subject it appears that a link should just work if you want it to display in Safari. I can't get them to work.
In pages I created the document and created a hyperlink to http://www.excite.com/ for testing purposes. I then exported the document as a pdf. The link works fine when the pdf is displayed in Preview. I then added the pdf to my app resources folder and loaded it into the UIWebView. The link does not work.
The UIWebView was created with IB and I have the Links property checked.
I am loading the pdf with the following code...
NSString *pdfPath =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[documents objectAtIndex:documentNumber]];
pdfUrl = [NSURL fileURLWithPath:pdfPath];
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
What more do I need to do?
For now I just would like the link to open in Safari. Eventually I will need to have links that allow me to link to other pdfs in my resource folder and display them in the same UIWebView. To do this I understand I will need to implement a UIWebViewDelegate.
Thanks,
John

Is that possible to cache UIWebView in iPhone?

I was using NYTimes iPhone application, I become bit curious when I notice that it cache UIwebview even though I didn't open that article.Does anyone have idea how to do that?
How NYTimes iPhone application doing offline reading?
Any help greatly appreciated.
Thanks,
Amit
I wrote the NYTimes app. Here are some details that you could have gotten by looking inside the app bundle.
I download the HTML for the articles, strip out whatever unsupported HTML and JS crud the producers stuffed in it and cache it in the backing store.
The article content is contained in a series of P tags (HTML fragment). I stuff that into a special HTML skeleton page that ships with the app. The static wrapper page also contains CSS and JS used to properly display the article and lazily load the images.
The image loading is really cool. The web view is notified when the images are ready. layout is not affected because I already know the sizes of the missing images.
You can use UIWebView to load html files stored locally on the iPhone.
NYTimes app is probably caching html and images in local storage.
Search google for "UIWebView local" and you get several useful hits.
I tried it out and it works great:
First, create a "view based application" and add a UIWebView to the NIB.
Second, add this code to your UIViewController code:
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[super viewDidLoad];
}
Third, add a file called "index.html" to your "Resources" folder in xcode and it will be displayed.
UPDATE:
Indeed, the complicated part of this is downloading the images and stylesheets for the webpage. Doing this server side is easy with Simple HTML Parser (and PHP). Just package everything in a zip and download to your iPhone.
Alternatively, you could do it locally with a C/C++/OBJC HTML parser (libxml2.2 is available on iOS). See this SO question Parsing HTML on the iPhone.
It's going to a bit of a project, so good luck.