[iPhone] open bundled Javascript/Images/CSS in an UIWebView

Hi,

Is it possible to open images, Javascript or CSS files directly from the application bundle within an HTML page loaded in an UIWebView ?
I don't mind if the HTML page itself is loaded from a website.

Thanks

MacBook, Mac OS X (10.5.5)

Posted on Oct 27, 2008 4:26 PM

Reply
9 replies

Oct 27, 2008 4:33 PM in response to jt_jam

It's either all local or not. You can't have a remote HTML file open local images, just like in a "real" web server.

If you bundle some HTML, CSS, images, etc. in your app's resources, you can see it all via UIWebView. Just make all href links and image src's be relative to the HTML file (most likely all will be in the same location - the app's main directory).

Oct 28, 2008 12:49 AM in response to RickMaddy

Thanks, then how do you open a local HTML file ? I do to it through the UIWebView loadData message like follows, it works but I cannot open a local image with a relative path.

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[ NSURL URLWithString:@"" ]];



If I understand well, you cannot have a remote HTML file to open local resources ?

Message was edited by: jt_jam

Oct 28, 2008 7:43 AM in response to jt_jam

Here's a better way to open a file from your app's resource directory in a UIWebView:


NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:basePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];


This should work with any html file or image in your resource directory.

And no, there is no way to have a remote HTML file open a local resource.

Oct 28, 2008 2:43 PM in response to RickMaddy

I work on an online application having a stable design (ie. background images, CSS files and Javascript libraries). But the data is stored on remote servers and I need to retrieve it.

One solution would have been to request remotely generated HTML files and use bundled CSS, Javascript, and images to avoid heavy downloads each time you run the application.

Since that's not possible, another option is to bundle HTML pages skeletons and retrieve data through AJAX, then fill in the HTML skeletons with the data.

What do you think ?

Jan 27, 2009 11:30 PM in response to jt_jam

Hi,

I am using UIWebView. Opening index.html file from local resource. File opening successfully. My html file using main2.js file to do some operations.
Problem is when i am calling JS functions from Obj C. Functions not getting called.
Even main2.js is loaded at index.html.
Is there any setting in UIWebView to enable javascript. Or html file is not able to locate JS file.

Thanks,
Ganesh Pisal

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

[iPhone] open bundled Javascript/Images/CSS in an UIWebView

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.