iPhone app with local HTML file doesn't support link - iphone

I want to create simple iPhone app with local HTML file.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[waWebView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
I made one webView and make it show index.html file in same directory with .m file.
Then make an another HTML file; index2.html, in same directory
I want to see index2.html through a link in index.html
So, in index.html, I put
View html 2
index.html is successfully shown, but above link doesn't work.
What do I need more? Help meT.T
In additional to this problem, this app doesn't support javascript code too...

the link is relativ.. you need to set the baseURL of your webView to the path containing the index html file
NSString *file = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *base = [NSURL fileURLWithPath:file.stringByRemovingLastPathComponent];
[webview loadHTMLString:text baseURL:base];

It is done by creating a NSURL using its fileURLWithPath and then a NSURLRequest and loading that rather loading the html itself:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[waWebView loadRequest:request];

You may need to add javascript to it for it to work. If you want to make a simple iPhone app just use app mobi(http://www.appmobi.com/) and they let you put css html and js files into a builder and it does the magic for you. Try that.
Hope I Helped
LucaSpeedStack

Related

PDF preview like Mail App

I am looking for a PDF preview feature similar to the one available when a PDF is received via mail in iPad mail app. It has options like Print/Mail and an Open In menu. Do you have any idea is it an apple's custom implementation or part of framework. I couldn't find in QLPreviewController and UIDocInteract framework.
its very simple to implement .. you will just load NSURLRequest with the NSURL that hold the pdf path into UIWebView.
for example :
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"iPadHIG" ofType:#"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
webView.scalesPageToFit = YES;

UIWebView load HTML file from local file system

How can I load a HTML from the local file system?
I have seen many questions like this but they are all referring to a file that is within a bundle. I would like to load from a path such as/Users/Abs/Documents/ - I think the ios simulator has access to this as I can save files to my local /Users/Abs/Documents/ directory.
But I don't know how to load a HTML file to the UIWebView.
How can I edit the below to achieve this:
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index.html" ofType #"html"]isDirectory:NO]]];
use below code it will work
NSString *filePath=[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
[webView loadData:[NSData dataWithContentsOfFile:filePath] MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:nil];
NSURL *url = [NSURL URLWithString:#"/Users/Abs/Documents/index.html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
Note that this is only useful when working in the simulator, because apps on a device are sandboxed and can only get to files in their bundle.

display image in html in webview from document directory

I would like to display an image in html which is being displayed on a UIWebView from the App's Document directory.
Can someone suggest me how to accomplish that?
This can help you
Using HTML and Local Images Within UIWebView
Link to resources inside WebView - iPhone
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can then refer to your images like this:
<img src="myimage.png">

Can't load JavaScript from Resource in UIWebView

I am loading a local HTML file into a UIWebView in iOS using this code
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
The page works fine, and the image linked within the HTML loads fine. However, the JavaScript linked off of the page is not loading.
The HTML, Image and JavaScript are all located in the same folder. (Resources/Html in my Project)
How can I get the JavaScript to load?
The problem was that XCode was showing me a warning:
warning: no rule to process file
'$(PROJECT_DIR)/html/orientation.js'
of type sourcecode.javascript for
architecture i386
This answer contains the solution. Basically, I had to tell XCode to not try and build the JavaScript files, and manually drag them to the "Copy Bundle Resources" directory.
The following code works for loading an html file which has javascript files in the web view
Here web is web view object and index is the html page
Web.delegate = self;
[Web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];

How do I make UIWebView show a pdf file from its own app?

I really need to make a UIWebView open up a specific PDF document that is in my project directory. I can't seem to find any tutorials on this. I found one, but the author was not specific about some of the code.
I have to assume it's got to be quite a simple bit of code.
Can someone help me code UIWebView to load a PDF?
Thanks.
Use pathForResource and fileURLWithPath:
NSString *path = [[NSBundle mainBundle] pathForResource:#"FileNameHere"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];