一、如何在APP里加载本地html文件内容:
首先准备一个html文件,比如内容如下:
title接下来,在APP里定义一个UIWebView,用来显示html文件内容:
//定义一个webview
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 375/WI * WIDTH, 667/HI * HEIGHT)];
//设置背景色
webView.backgroundColor = [UIColor clearColor];
//加载名为index.html的文件
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
//控制缩放以适应屏幕
[webView setScalesPageToFit:YES];
//将webview添加到主屏幕
[self.view addSubview:webView];
上面的代码实现了加载html的内容,如果需要点击html的链接,跳转到APP页面,需要加上下面这一行设置:
webView.delegate = self;
并且实现如下函数:
- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked) {//点击链接
//这里实现跳转的代码
//XXX
return NO; // 返回NO说明链接不跳转
}
return YES;
}
这样就完成了点击链接跳转到APP页面的功能。
注意本实现中未对链接进行区分,所以如果HTML中存在多个链接,点击后都会跳转到我们设置的页面。