使用UIWebView显示html文件

创建了一个UIWebView,使用HTML文件来显示一些内容。但是运行之后,没有显示内容而是显示了HTML全部编码。不知道应该怎么解决?

UIWebView *ingradients= [[UIWebView alloc]init];
    [ingradients setFrame:CGRectMake(10, 170, 300, 300)];
    [ingradients loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"htmlfile" ofType:@"html"]isDirectory:NO]]];
    ingradients.delegate=self;
    [self.view addSubview:ingradients];

htmlfile.html 内容

<html>
<body>
<p><strong>Ingredients</strong></p>
</body>
</html>

结果没有显示成粗体的 Ingredients ,而是将代码都全部显示出来了。

测试了你的代码是正确的,问题出在你的htmlfile.html文件里。请用类似UE等工具打开确保里面的内容是:

<html>
<body>
<p><strong>Ingredients</strong></p>
</body>
</html>

或者直接用Chrome打开htmlfile.html再查看源码。