可能最好使用NSString并加载html文件,如下所示:
Objective-C的NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"];NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];[webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
迅速let htmlFile = NSBundle.mainBundle().pathForResource("fileName", ofType: "html")let html = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)webView.loadHTMLString(html!, baseURL: nil)
Swift 3几乎没有变化:let htmlFile = Bundle.main.path(forResource: "intro", ofType: "html")let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)webView.loadHTMLString(html!, baseURL: nil)
你试过了吗?
还要检查是否通过pathForResource:ofType:inDirectory调用找到了资源。