UIWebview iPhone loading bundle resources - iphone

I'm trying to load images from the bundle to a uiwebview.
the code i'm currently using is
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
when i open the view the application closes and opens safari and an error message pops with 'Safari cannot open the file. The error was: "file is directory".'
if i set the baseURL as nil then the page loads correctly (although without the images).
Any suggestions?

Just installed the application onto an actual iphone - and it works correctly as expected, just in the iphone simulator UIWebView and bundle path doesn't work correctly.

Related

iPhone app with local HTML file doesn't support link

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

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;

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]]];

iPhone - How to preload a local file in a UIWebView?

Is it possible to preload content of a local file with embedded images for a UIWebView?
This is how I'm loading the data inside the UIWebView:
NSString *urlString = [[NSBundle mainBundle] pathForResource:#"info" ofType:#"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:urlString encoding:NSUTF8StringEncoding error:nil];
[myWebView loadHTMLString:htmlContent baseURL:nil];
But the UIWebView still needs some time to display the content. Is there any way to put this content into the browser cache on app start?
If you create the browser object on app start and load the HTML string immediately, it should work fine to display the view later.
Another option would be to allocate it when necessary, but not actually show the view until it's done loading (wait for - (void)webViewDidFinishLoad:(UIWebView *)webView to be called on the web view's delegate before showing it).
Also, would it make more sense to call
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:urlString]]];
than using the loadHTMLString: method? That way, you don't have to load the HTML string into memory yourself and the web view can load it directly from disk.

How to check the URL of a WebView?

I have a Subview that load a Webview. In this Webview I load a file HTML locally in the Documents folder of my App. I need to check if the webview load another HTML file. Specifically:
Webview load "index.html" locally in my Documents folder
After 7 days a Javascript load another page with location.href= "index2.html",
When I load this WebView I need to check if the current URL is index.html or index2.html, if is the second I must change Subview!
How can I do this?
NSString *currentUrl = [[[yourWebView request] URL] absoluteString];
Use this NSURL method [NSURL fileURLWithPath:#"some/path/to/your/file" isDirectory:NO] and pass that to a NSURLRequest which is then passed to your UIWebView. You can get the path to your resource by using the [[NSBundle mainBundle] pathForResource...] methods.
NSURL *url = myWebView.request.URL;