iOS 嵌入本地H5
估计很多小伙伴都会遇到项目中要加载H5而自己又不知道怎么做,本篇文章将为小伙伴们答疑解惑,废话不多少,直接开始。
1、先创建好html、css、js


按照图示先创建后缀名为.html .css .js的文件
2、插入html、css、js代码
在.html文件中加入如下代码

在css文件中加入

在js中加入

3、在ViewController中创建UIWebview并添加代理;
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];//由于是测试所以就铺满全屏
webView.delegate = self;//代理
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *url = [NSURL fileURLWithPath:path];
NSString *htmlStr = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];//加载本地的html文件
NSString *htmlString = [NSString stringWithContentsOfFile:htmlStr encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:url];//加载html
[self.view addSubview:webView];//添加到view上
4、代理方法
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
}
//开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
//捕捉HTML的交互,页面开始加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSLog(@"url : %@", [NSString stringWithFormat:@"%@", url]);
return YES;
}
最后的效果图

以上就是加载本地html的简单的方法,小伙伴们赶紧去试试吧