background preloader

Usual UiWebView usage

Facebook Twitter

Loading Local Files into UIWebView. I thought i would make this a separate post to the Build your very own Web Browser!

Loading Local Files into UIWebView

Tutorial as it can be used for many different purposes and it took me a bit to figure it out. For my example I am going to be trying to use a file in my local bundle in a UIWebView, my file is going to be called “sample.jpg” and it will be in the root folder of my project, this will work for any folder though. I will show you the code and then I will explain it. NSString *imagePath = [[NSBundle mainBundle] resourcePath]; imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"]; imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; NSString *HTMLData = @" <h1>Hello this is a test</h1><img src="sample.jpg" alt="" width="100" height="100" />"; [webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]]; Line 1 This will get the path to the main bundle root folder.

UiWebView - Local Files, Remote HTML - Any takers. I'm looking for a way to use UiWebView to load a remote web site, and on that site I'd like any images with a .png or .gif (referenced relatively) extension to be loaded from the root of the app bundle.

UiWebView - Local Files, Remote HTML - Any takers

For example, the app loads a remote page That index.html file tries to load an image with say <img src="test.png">. Instead of the app trying to download "test.png" from the web site, i'd like it to just load the packaged "test.png" file that is stored in the root of the app bundle. I've got no problem loading up a remote site, but I just cant figure out how to get that remote site to load up local image files.

I know that there has to be SOME kind of work-around, if you take a look at the Bank of America application that you can download for free in the app store, it uses UiWebView to load www.bankofamerica.com/mobile and somehow it references the files in the root of the app bundle. Post edited by John.Ohl on. How To Read a File From Your Application Bundle. Apr 07 First you need to add your file to the Resources folder of your Xcode project.

How To Read a File From Your Application Bundle

Then you can access the file like this (assuming the file is called MyFile.txt): NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"]; NSData *myData = [NSData dataWithContentsOfFile:filePath]; if (myData) { } NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"]; NSData *myData = [NSData dataWithContentsOfFile:filePath]; if (myData) { // do something useful } Here’s a complete example reading a help text file into a UIWebView. [iPhone] open bundled. UIWebView – Loading External Images and CSS. Dec 12 Among the most popular posts on this blog have been those about UIWebView. (The fact that it’s the #1 search result on Google for the term UIWebView might help.

Here’s I’d like to revisit the problem of displaying local images in a UIWebView. In this post I presented a little hack to embed small images into the HTML. While this may work in some cases, it’s far from an optimal solution. Using relative paths or file: paths to refer to images does not work with UIWebView. NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [webView loadHTMLString:htmlString baseURL:baseURL]; You can then refer to your images like this: Or from within CSS like this: background-image: url(loading.gif) It’s important to note that images inside your application bundle are at the root of the bundle, even if you place them in an Images directory in your project.

Using iPhone UIWebView Class with local CSS & JavaScript resources. This post will cover the basic setup and creation of an application with web content for iPhone that will load local CSS files and some useful JavaScript functions.

Using iPhone UIWebView Class with local CSS & JavaScript resources

Most of these hints I found partially in different blogs and forums listed in the reference section. The idea is to collect all them together. You can use the following technique to create a more attractive application design. Creating an application using UIWebView control is not the focus of this post.

Here is a helpful beginner’s tutorial for creating an application using UIWebView. UIWebView load Our work will start with a little change in calling load method of the UIWebView instance. [[webView graphWebView] loadRequest:requestObj]; we use the loadHTMLString method, providing an NSString object that contains HTML code of our page as a parameter: UIWebView Tutorial.