UIWebView display locally stored website (HTML, images, Javascript)

后端 未结 6 2156
栀梦
栀梦 2020-12-08 12:50

I\'ve looked EVERYWHERE for this, can\'t find anything. I basically need to store an entire website inside of my iPhone for some interesting reasons. Regardless I can\'t fig

6条回答
  •  无人及你
    2020-12-08 13:12

    The problem is that this call:

    NSURL *url = [NSURL fileURLWithPath:path];
    

    doesn't setup a baseURL and so relative paths in the .html file for things like javascript, css, images etc don't work.

    Instead use this:

    url = [NSURL URLWithString: [path lastPathComponent] 
                 relativeToURL: [NSURL fileURLWithPath: [path stringByDeletingLastPathComponent] 
                                           isDirectory: YES]];
    

    and then things like "styles.css" in the index.html file will be found IFF they are copied into the bundle next to the .html file.

提交回复
热议问题