Cocoa WebView - Loading a local HTML page
After browsing quite a bit, I still can't figure this out. I've added a HTML page and its images directory into my project Resources group in Xcode (Copied them over).
When I try to load the WebView with the following code, the text is displayed fine, but the images aren't loaded.
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 about the delay, here's 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 it was an issue with the path. Apparently <img src = "images/bg.png">
doesn't work, but if I copy bg.png to the root directory and say <img src = "bg.png">
everything works fine. I'm still not sure where I'm going wrong with this.
Consider doing the following:
- Instead of getting the resource path and stapling a subpath onto it yourself, ask the bundle to give you the correct path.
- Instead of handling a path at all, ask the bundle to give you the correct URL. (Requires Mac OS X 10.6 or later.)
- Instead of injecting HTML contents directly into the frame, ask the WebView to load the URL.
This reduces your code to two lines that, if the HTML code is correct, will work.
I think that the problem here is to do with how files are copied to the resources directory during the build phase. Any folders that are shown as groups in Xcode will be flattened when they are copied across. If you are adding an entire website folder structure to your project you must select "Create folder references for any added folders" when adding the structure.
An example: I have an html file that references an image in a folder of the same name. I add all this into my project selecting 'folder references':
The files will look like this in my project (note the blue folder):
When the project is built the web contents folder will be maintained:
There is no need to start setting base URLs or loading with strings. The web page can now be successfully loaded like this:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"LocalTestWeb" withExtension:@"html"];
[[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
This code works for me (only having issues with Canvas):
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];
You will need to get the image file data in base64 string. And put that string in img tag as below.
<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />
See this for more details: http://danielmclaren.com/node/90
精彩评论