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

    此代码适用于我(仅与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];
    
    链接地址: http://www.djcxy.com/p/34812.html

    上一篇: Android:带自定义字体的WebView

    下一篇: 加载本地HTML页面