UIWebView如何打开本地的网页

来源:互联网 发布:上海师范大学网络 编辑:程序博客网 时间:2024/05/17 06:06
1,在xib文件中加入UIWebView控件。连接Controller中的对应的IBOutlet。
@interface AboutViewController : UIViewController {IBOutlet UIWebView *webView;}@property(nonatomic,retain)  UIWebView *webView;@end
2,将html文件(假设为test.html)加到工程目录中。3,在需要加载网页的地方比如在viewDidLoad里面,实现如下:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad {[super viewDidLoad];NSString *fullPath = [NSBundle pathForResource:@"test" ofType:@"html" inDirectory:[[NSBundle mainBundle] bundlePath]];[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];}
4,加入下面一段代码,使其点击链接时,跳出app到网页(可选):
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;{NSURL *requestURL =[[request URL ] retain ];if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ])   && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];}[requestURL release];return YES;}


我按照步骤3中的代码设置了加载的文件,但是运行后发现只有内容显示了出来,图片和样式都没有加载上,我利用nslog打印了fullpath,html文件的确是在.app文件夹中,我又到文件夹下查找资源文件,发现了问题,所有app文件夹下的文件都是发在平级的,其中不再有css和image这样的文件夹,所以我修改了一下html文件中引用资源的语句,网页就正常了。