做一些项目的时候会加载一些网页累的文件,调取详情页的时候会加载<p>标签富文本内容显示,需要用到WKWebView加载显示,iOS已经禁用UIWebView的使用了。
一、引入web头文件库
#import "FileHtmlViewController.h"
//引入WebKit
#import <WebKit/WebKit.h>
@interface FileHtmlViewController ()<WKUIDelegate,WKNavigationDelegate>
@property (nonatomic,strong) WKWebView * webView; //声明
@end
二、上代码
#pragma mark - 第一种方法加载网页 测试百度 https://www.baidu.com/
-(void)createUI{
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
[self.view addSubview:_webView];
_webView.navigationDelegate = self; //实现代理
// 1、url打开
// [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_urlstring]]]];
// iOS 中如何显示带HTML<p>标签的富文本 https://www.jianshu.com/p/8e5dc2279f59?from=singlemessage
[_webView loadHTMLString:_urlstring baseURL:nil]; //iOS 中如何显示带HTML标签的富文本
}
[_webView loadHTMLString:_urlstring baseURL:nil];
_urlstring->这个是从上一个页面传过来的文本参数进行web加载显示
三、WKWebView,下面这段代码附加的,因为在加载url页面是显示空白,需要校验凭证,这句代码亲测有用,特此记录
#pragma mark - 需要响应身份验证时调用 同样在block中需要传入用户身份凭证
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
// 判断服务器采用的验证方法
if (challenge.previousFailureCount == 0) {
// 如果没有错误的情况下 创建一个凭证,并使用证书
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
} else {
// 验证失败,取消本次验证
completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
}
} else {
completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
}
}
四、WKWebView 展示HTML字体小的问题
#pragma mark - webView delegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
#pragma mark -禁止用户选择
[webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
[webView evaluateJavaScript:@"document.activeElement.blur();" completionHandler:nil];
#pragma mark -适当增大字体大小 我设置的是300字体大小正好适应屏幕
[webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'" completionHandler:nil];
}
示例1
示例2
其他的一些方法可以实现自定设定,我没搞,根据项目需求业务来,怎么简单怎么来吧!
五、WKWebView中包含了两个delegate。WKNavigationDelegate和WKUIDelegate。
如命名,这个是WKWebView的导航的代理。它控制了WKWebView在加载一个页面流程中的各个关键时间节点的。相当于WKWebView加载的生命周期方法。
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"页面开始加载");
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
NSLog(@"页面开始返回");
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSLog(@"页面完成加载");
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"页面加载失败");
}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"页面重定向");
}
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSLog(@"%@",navigationResponse.response.URL.absoluteString);
//允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"%@",navigationAction.request.URL.absoluteString);
//允许跳转
decisionHandler(WKNavigationActionPolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationActionPolicyCancel);
}