WKWebView

一.引言

UIWebView是在iOS2时发布的,WKWebView是在Apple的WWDC 2014随iOS 8和OS X 10.10发布的,WKWebView将逐步取代笨重的UIWebView,加载速度更快,占用内存少,优化简单。

二.WKWebView新特性

1.在性能、稳定性、功能方面有很大提升(最直观的体现就是加载网页是占用的内存);

2.允许JavaScript的Nitro库加载并使用(UIWebView中限制);

3.支持了更多的HTML5特性;

4.高达60fps的滚动刷新率以及内置手势;

5.提供常用的属性,如加载网页进度的属性estimatedProgres

6.将UIWebViewDelegate与UIWebView重构成了14类与3个协议查看苹果官方文档;

三.WKWebView的使用

导入Wbkit这个类库
引用#import

//初始化
WKWebView*webView = [[WKWebViewalloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];

//加载网页
[webView loadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:@"https://www.baidu.com"]]];

WKWebView有两个委托
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;

//在发送请求之前,决定是否跳转
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler;

WKUIDelegate;

//创建一个新的WebVeiw
- (nullableWKWebView*)webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures;

//WebVeiw关闭(9.0中的方法)
- (void)webViewDidClose:(WKWebView*)webViewNS_AVAILABLE(10_11,9_0);

//显示一个JS的Alert(与JS交互)
- (void)webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(void))completionHandler;

//弹出一个输入框(与JS交互的)
- (void)webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt defaultText:(nullableNSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(NSString* __nullableresult))completionHandler;

//显示一个确认框(JS的)
- (void)webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(BOOLresult))completionHandler;

你可能感兴趣的:(WKWebView)