iOS读取本地Html资源和一般网站

来源:互联网 发布:侧铣头编程讲解 编辑:程序博客网 时间:2024/06/10 02:36

目前的移动开发中,一般在原生的App里面都会嵌套一些Html或者直接就是网页,下面介绍一下WebView加载本地Html资源和网站的方法。

本地Html:

NSString *filePath = [[NSBundle mainBundle] pathForResource:urlStr ofType:nil];NSURL * url = [[NSURL alloc] initFileURLWithPath:filePath];request = [NSURLRequest requestWithURL:url];[_webView loadRequest:request];
其中urlStr为你的本地html路径,例如:@“Html/dist/index.html”,本方法加载html,要求有两个,一是吧html文件夹拖进工程时,选择create folder和copy item if needed;二是必须把文件夹加入到Copy Bundle Resources。如下图:


加载网站:

这个就比较简单了,代码如下。

NSURL *url = [[NSURL alloc] initWithString:urlStr];request = [NSURLRequest requestWithURL:url];[_webView loadRequest:request];
Civil_Pro,就是简单粗暴的贴代码。