UIWebView占用过多内存,且内存峰值更是夸张。
WKWebView网页加载速度也有提升
1、更多的支持HTML5的特性
2、官方宣称的高达60fps的滚动刷新率以及内置手势
3、Safari相同的JavaScript引擎,且允许JavaScript的Nitro库加载并使用(UIWebView中限制);
4、将UIWebViewDelegate与UIWebView拆分成了14类与3个协议(官方文档说明)
5、占用更少的内存,在性能、稳定性、功能方面有很大提升;
另外增加了加载进度属性:estimatedProgress
首先需要引入WebKit库
WKWebView有两个delegate,
- WKUIDelegate
- WKNavigationDelegate。
WKNavigationDelegate 主要处理一些页面跳转、加载处理操作,
WKUIDelegate 主要处理JS脚本,确认框,警告框等。
// 默认初始化
- (instancetype)initWithFrame:(CGRect)frame;
// 根据对webview的相关配置,进行初始化
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
WKNavigationDelegate
//该代理提供的方法,可以用来追踪加载过程(页面开始加载、加载完成、加载失败)、决定是否执行跳转。
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
}
//页面跳转的代理方法有三种,分为(收到跳转与决定是否跳转两种)
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
}
// 在收到响应后,决定是否跳转
- (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);
}
WKUIDelegate
#pragma mark - WKUIDelegate
// 创建一个新的WebView
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
return [[WKWebView alloc]init];
}
// 输入框
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler{
completionHandler(@"http");
}
// 确认框
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{
completionHandler(YES);
}
// 警告框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
NSLog(@"%@",message);
completionHandler();
}
WKWebView与JS的交互:
可以直接添加对约定的ScriptMessage(直译为 脚本信息,本文暂定为JS事件)的监听,即可实现JS调用OC。所有的操作都是通过WKUserContentController来处理的
如何获取WKUserContentController
WKUserContentController 是 WKWebViewConfiguration 的属性,
WKWebViewConfiguration 是 WKWebView 的属性(也就是在WKWebView实例化的时候传入的configuration)
WKUserContentController *conntentController = self.webView.configuration.userContentController;
JS调用OC:
首先在JS代码中加入对事先约定好的 ScriptMessage(JS事件)的调用
window.webkit.messageHandlers.<事件名>.postMessage(需要传递的数据)
OC端添加一个名为 @”closeWindow” 的 JS的监听:
[conntentController addScriptMessageHandler:self name:@"closeWindow"];
[conntentController removeScriptMessageHandlerForName:@"closeWindow"];
OC调用JS:
//与UIWebView一样WKWebView可直接调用JS方法
[self.webView evaluateJavaScript:@"function('action')" completionHandler:nil];
支持HTTP:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
加载本地html:
NSURL *filePath = [[NSBundle mainBundle] URLForResource:@"www/index.html" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:filePath];
[_wkWebView loadRequest:request];
加载HTML不显示
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"WebFile"];
文件夹颜色变蓝色: 更改导入的方式
参考:
https://www.jianshu.com/p/4fa8c4eb1316
http://www.cocoachina.com/ios/20171019/20849.html
http://www.cocoachina.com/ios/20170908/20518.html
https://www.jianshu.com/p/870dba42ec15
网友评论