iOS8之后,苹果推出了新框架Wekkit
,提供了替换UIWebView
的组件WKWebView
。各类UIWebView
的问题没有了,速度更快了,占用内存少了,一句话,WKWebView
是App内部加载网页的最佳选择!html
先看下 WKWebView
的特性:java
而后从如下几个方面说下WKWebView
的基本用法:ios
WKUIDelegate
协议JS
代码JS
代码JS
调用App注册过的方法加载网页或HTML代码的方式与UIWebView
相同,代码示例以下:git
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds]; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]]; [self.view addSubview:webView];
用来追踪加载过程(页面开始加载、加载完成、加载失败)的方法:github
// 页面开始加载时调用 - (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;
页面跳转的代理方法:web
// 接收到服务器跳转请求以后调用 - (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
协议这个协议主要用于WKWebView
处理web界面的三种提示框(警告框、确认框、输入框),下面是警告框的例子:数组
/** * web界面中有弹出警告框时调用 * * @param webView 实现该代理的webview * @param message 警告框中的内容 * @param frame 主窗口 * @param completionHandler 警告框消失调用 */ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(void (^)())completionHandler;
JS
代码用于在客户端内部加入JS
代码,并执行,示例以下:服务器
// 图片缩放的js代码 NSString *js = @"var count = document.images.length;for (var i = 0; i < count; i++) {var image = document.images[i];image.style.width=320;};window.alert('找到' + count + '张图');"; // 根据JS字符串初始化WKUserScript对象 WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; // 根据生成的WKUserScript对象,初始化WKWebViewConfiguration WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; [config.userContentController addUserScript:script]; _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config]; [_webView loadHTMLString:@"<head></head><imgea src='http://www.nsu.edu.cn/v/2014v3/img/background/3.jpg' />"baseURL:nil]; [self.view addSubview:_webView];
JS
代码用户调用用JS
写过的代码,通常指服务端开发的:app
//javaScriptString是JS方法名,completionHandler是异步回调block [self.webView evaluateJavaScript:javaScriptString completionHandler:completionHandler];
JS
调用App注册过的方法再WKWebView
里面注册供JS调用的方法,是经过WKUserContentController
类下面的方法:框架
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
scriptMessageHandler
是代理回调,JS
调用name
方法后,OC
会调用scriptMessageHandler
指定的对象。
JS
在调用OC
注册方法的时候要用下面的方式:
window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
注意,name(方法名)是放在中间的,messageBody只能是一个对象,若是要传多个值,须要封装成数组,或者字典。整个示例以下:
//OC注册供JS调用的方法 [[_webView configuration].userContentController addScriptMessageHandler:self name:@"closeMe"]; //OC在JS调用方法作的处理 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { NSLog(@"JS 调用了 %@ 方法,传回参数 %@",message.name,message.body); } //JS调用 window.webkit.messageHandlers.closeMe.postMessage(null);
若是你在self
的dealloc
打个断点,会发现self
没有释放!这显然是不行的!谷歌后看到一种解决方法,以下:
@interface WeakScriptMessageDelegate : NSObject<WKScriptMessageHandler> @property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate; - (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate; @end @implementation WeakScriptMessageDelegate - (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate { self = [super init]; if (self) { _scriptDelegate = scriptDelegate; } return self; } - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { [self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message]; } @end
思路是另外建立一个代理对象,而后经过代理对象回调指定的self
,
WKUserContentController *userContentController = [[WKUserContentController alloc] init]; [userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"closeMe"];
运行代码,self
释放了,WeakScriptMessageDelegate
却没有释放啊啊啊!
还需在self
的dealloc
里面 添加这样一句代码:
[[_webView configuration].userContentController removeScriptMessageHandlerForName:@"closeMe"];
OK,圆满解决问题!
目前,大多数App须要支持iOS7以上的版本,而WKWebView
只在iOS8后才能用,因此须要一个兼容性方案,既iOS7下用UIWebView
,iOS8后用WKWebView
。这个库提供了这种兼容性方案:https://github.com/wangyangcc/IMYWebView
以上部份内容参考自:http://www.brighttj.com/ios/ios-wkwebview-new-features-and-use.html
在本人播客中地址:http://www.wangyangdev.com/2015/11/13/使用WKWebView替换UIWebView/
文/wangyangyang(简书做者) 原文连接:http://www.jianshu.com/p/6ba2507445e4 著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。