在 uiwebview 中使用本地 html 从相对路径加载资源

我有一个非常简单的 iOS 应用程序,它带有一个 uiwebview 加载一个非常简单的测试页面(test.html) :

<html>
<body>
<img src="img/myimage.png" />
</body>
</html>

I load this test.html file into my web view:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

如果我引用没有相对路径的图像,并将引用的图像放在 XCode 中 Targets-> Copy Bundle Resources 下的根路径中,那么这种方法可以很好地工作,但是我无法让它使用 html 文件中显示的相对路径。一定有办法做到这一点,我有很多图片,css,javascript 文件,我想加载到网络视图,我不想有一切都在根,并不得不改变所有的引用,在我的网络应用程序。

118675 次浏览

我把所有东西都塞进一行(我知道很糟糕) ,没有任何问题:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test"
ofType:@"html"]
isDirectory:NO]]];

I've gone back and tested and re-tested this, it appears that the only way I can get it to work (since I have some files in the img folder and some in js/css, etc...) is not to use a relative path to my image in the html and have everything referenced to the bundle folder. What a shame :(.

<img src="myimage.png" />

This is how to load/use a local html with relative references.

  1. Drag the resource into your xcode project (I dragged a folder named www from my finder window), you will get two options "create groups for any added folders" and "create folders references for any added folders".
  2. 选择“ create file reference. .”选项。
  3. 使用下面给定的代码。它应该像魔咒一样工作。

    NSURL * url = [ NSURL fileURLWithPath: [[ NSBundlemainBundle ] pathForResource:@“ index”ofType:@“ html”inDirectory:@“ www”] ;
    [ webview loadRequest: [ NSULRequest requestWithURL: url ] ;

现在 html 中的所有相关链接(如 img/. gif,js/.js)都应该得到解析。

Swift 3

    if let path = Bundle.main.path(forResource: "dados", ofType: "html", inDirectory: "root") {
webView.load( URLRequest(url: URL(fileURLWithPath: path)) )
}

我只是这样做:

    UIWebView *webView = [[[UIWebView alloc] init] autorelease];


NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

其中“ index.html”相对引用图像、 CSS、 javascript 等。

斯威夫特:

 func pathForResource(  name: String?,
ofType ext: String?,
inDirectory subpath: String?) -> String?  {


// **name:** Name of Hmtl
// **ofType ext:** extension for type of file. In this case "html"
// **inDirectory subpath:** the folder where are the file.
//    In this case the file is in root folder


let path = NSBundle.mainBundle().pathForResource(             "dados",
ofType:      "html",
inDirectory: "root")
var requestURL = NSURL(string:path!)
var request = NSURLRequest(URL:requestURL)


webView.loadRequest(request)
}

快速回答2。

UIWebView Class Reference建议不要使用 webView.loadRequest (request) :

不要使用此方法加载本地 HTML 文件; 而是使用 BaseURL: .

在这个解决方案中,html 被读入一个字符串。Html 的 url 用于计算路径,并将其作为基 URL 传递。

let url = bundle.URLForResource("index", withExtension: "html", subdirectory: "htmlFileFolder")
let html = try String(contentsOfURL: url)
let base = url.URLByDeletingLastPathComponent
webView.loadHTMLString(html, baseURL: base)

@ sdbrain 在 Swift 3中的回答:

    let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "www")!)
webView.loadRequest(NSURLRequest.init(url: url) as URLRequest)

在 Swift 3.01中使用 WKWebView:

let localURL = URL.init(fileURLWithPath: Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "CWP")!)
myWebView.load(NSURLRequest.init(url: localURL) as URLRequest)

这可以根据3.01中一些更好的语法变化进行调整,并保持目录结构不变,以便可以嵌入相关的 HTML 文件。