Cocoa WebView - Download Local HTML Page

After watching a bit, I still can not figure it out. I added an HTML page and its image directory to my project resource group in Xcode (copied them).

When I try to load the WebView using the following code, the text appears in order, but the images do not load.

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"index.html"]; NSString *htmlContent = [NSString stringWithContentsOfFile:path]; NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; [[tempWebView mainFrame] loadHTMLString:htmlContent baseURL:url]; 

EDIT: Sorry for the delay, here are some basic html that failed.

 <html> <body> <img src="images/bg.png"></img> </body> </html> 

And my edited code looks like this:

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

EDIT2: Just realized this is a problem with this path. Apparently, <img src = "images/bg.png"> does not work, but if I copy bg.png to the root directory and say <img src = "bg.png"> everything will be fine. I'm still not sure where I am going wrong.

+7
cocoa webview
source share
4 answers

Consider the following:

This will reduce your code to two lines, which, if the HTML is correct, will work.

+8
source share

I think the problem here is how the files are copied to the resource directory during the build phase. Any folders that appear as groups in Xcode will be smoothed when copied. If you add the entire folder structure of the website to your project, you must select "Create folder links for any folders added" when adding the structure.

Example: I have an html file that refers to an image in a folder with the same name. I add all this to my project by selecting "folder links":

Adding the files

The files will look like this in my project (pay attention to the blue folder):

Project tree

When the project is created, the web content folder will be saved:

Resources folder in the bundle

There is no need to start setting base URLs or loading strings. Now the web page can be loaded successfully as follows:

 NSURL *url = [[NSBundle mainBundle] URLForResource:@"LocalTestWeb" withExtension:@"html"]; [[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]]; 
+4
source share

This code works for me (only with Canvas issues):

 NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"html_files_folder"]; NSURL* fileURL = [NSURL fileURLWithPath:filePath]; NSURLRequest* request = [NSURLRequest requestWithURL:fileURL]; [[webView mainFrame] loadRequest:request]; 
+3
source share

You will need to get the image file data in the base64 string. And put this line in the img tag as shown below.

 <img src="data:image/png;base64,iVBORw0KGgoAAAANS..." /> 

See this for more details: http://danielmclaren.com/node/90

+1
source share

All Articles