使用如下方式试试
NSURL *baseURL = [NSURL fileURLWIthPath:[[NSBundle mainBundle] bundlePath]];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSError *error;
NSString *html = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error: error];
if (!error) {
[webView loadHTMLString:html baseURL:baseURL];
}else {
NSLog(@"error:%@",error);
}
NSURL *baseURL = [NSURL fileURLWIthPath:[[NSBundle mainBundle] bundlePath]];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSError *error;
NSString *html = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error: error];
if (!error) {
[webView loadHTMLString:html baseURL:baseURL];
}else {
NSLog(@"error:%@",error);
}
显示如下:html:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body><p>弹弹弹弹弹弹弹弹弹</p>
</body>
</html>
--------------------------
请赐教!
一:将 url 部分替换成 http://www.baidu.com
看看能不能显示出来。
如果显示不出来的话, 你的显示方式有问题。如果可以显示网页的话,那么就是读取部分有问题,可以换一种方式来显示看看。一个 webView 显示的方式有很多种的,你可以尝试一下。NSBundle* bundle = [NSBundle mainBundle];
NSString* resPath = [bunder resourcePath];
NSString* filePath = [resPath stringByAppendPathComponent:@"test.html"];
[WebView loadHTMLString:[NSString stringWithContentsOfFile:filePath] baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];
[self.view addSubview: webView];
ViewController.m总的代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview: webView];
// get the model which is a html file for the webView
NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"html"];
NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
// load the html file to webView
[webView loadHTMLString:htmlCont baseURL:nil];
}在此感谢各位热心的帮助。