微信 iOS 客户端将于3月1日前逐步升级为WKWebview内核,2014年推出的WKWebView将替代UIKit 中笨重,内存泄漏的UIWebView,其拥有滚动刷新率与safari下相同的JavaScript 引擎优势。
常用API
//webview配置
@property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration;
//页面标题,支持KVO
@property (nullable, nonatomic, readonly, copy) NSString *title;
//当前请求的URL,支持KVO
@property (nullable, nonatomic, readonly, copy) NSURL *URL;
//标识当前是否正在加载内容中,支持KVO
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
// 历史访问列表;
@property (nonatomic, readonly, strong) WKBackForwardList *backForwardList;
//标识是否支持左右swipe手势,是否可以前进后退,默认NO
@property (nonatomic) BOOL allowsBackForwardNavigationGestures;
//当前加载进度【0,1】
@property (nonatomic, readonly) double estimatedProgress;
//标识页面中所有资源是否台安县过安全加密连接来加载,支持KVO
@property (nonatomic, readonly) BOOL hasOnlySecureContent;
//是否可以执行goback操作,支持KVO
@property (nonatomic, readonly) BOOL canGoBack;
//是否可以执行gofarward操作,支持KVo
@property (nonatomic, readonly) BOOL canGoForward;
//返回上一页(如果不能,则什么都不做)
- (nullable WKNavigation *)goBack;
//进入下一页(如果不能,则什么都不做)
- (nullable WKNavigation *)goForward;
//重新载入页面
- (nullable WKNavigation *)reload;
//重新载入原始页面
- (nullable WKNavigation *)reloadFromOrigin;
//停止加载数据
- (void)stopLoading;
//执行JS 代码
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
JavaScript配置相关
//所有添加的WKUserScript都在这里
@property (nonatomic, readonly, copy) NSArray<WKUserScript *> *userScripts;
//注入JS
- (void)addUserScript:(WKUserScript *)userScript;
//移除所有注入的JS
- (void)removeAllUserScripts;
//JS调用原生方法可能过下面方法
//添加scriptMessageHandler到所有frames中,都可以通过
//window.webkit.messageHandlers.<name>.postMessage(<messagebody>)
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
//根据name移除所有注入的scriptMessageHandler
- (void)removeScriptMessageHandlerForName:(NSString *)name;
加载相关
//从URLRequest加载
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
//从本地加载,仅限iOS 9以上
- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL API_AVAILABLE(macosx(10.11), ios(9.0));
//从HTML字符串加载
- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
//从二进制流中加载
- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL API_AVAILABLE(macosx(10.11), ios(9.0));
代理相关
WKNavigationDelegate
//在发送请求前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
//收到服务器的响应,根据response相关信息,决定是否跳转,
//decisionHandler必须调用以决定是否跳转。
//WKNavigationResponsePolicyCancel-取消跳转
//WKNavigationResponsePolicyAllow-允许跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
//准备加载页面 == shouldStartLoadWithRequest
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
//开始获取页面内容
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;
//页面加载完成== didFinishLoad
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
//页面内容加载完成,iOS 9
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView API_AVAILABLE(macosx(10.11), ios(9.0));
//页面跳转失败 == didFailLoadWithError
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
//页面加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
WKUIDelegate
// 创建新的WebView;
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {}
// 拦截警告框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {}
// 拦截确认框
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {}
基本使用
@interface ViewController ()<WKNavigationDelegate,WKScriptMessageHandler>
@property(nonatomic,retain)WKWebView *webView;
@property (nonatomic,retain)UIProgressView *progressView;
@property (nonatomic,retain)WKUserContentController *userContentController;
@end
-(WKWebView*)webView {
if (!_webView) {
//先实例化配置类 以前UIWebView的属性有的放到了这里
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
//注册供js调用的方法
_userContentController =[[WKUserContentController alloc]init];
//弹出登录
[_userContentController addScriptMessageHandler:self name:@"loginVC"];
//进入详情页
[_userContentController addScriptMessageHandler:self name:@"gotodetailVC"];
configuration.userContentController = _userContentController;
configuration.preferences.javaScriptEnabled = YES;//打开js交互
//创建webView
_webView = [[WKWebView alloc]initWithFrame:[UIScreen mainScreen].bounds configuration:configuration];
_webView.navigationDelegate = self;
_webView.allowsLinkPreview = YES;//允许预览链接
_webView.allowsBackForwardNavigationGestures = YES;//允许滑动返回
//创建进度条,WkWebView自带预估进度
_progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0.5, 0, _webView.frame.size.width, 2)];
_progressView.tintColor = [UIColor blueColor];
_progressView.trackTintColor = [UIColor whiteColor];
[_webView addSubview:_progressView];
//添加观察者拿到进度
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
//创建请求
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
//可变的请求可以设置缓存
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
//加载
[_webView loadRequest:request];
}
return _webView;
}
//设置进度条
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
_progressView.hidden = NO;
CGFloat progress = [change[@"new"] floatValue];
[_progressView setProgress:progress];
if (progress == 1.0) {
_progressView.hidden = YES;
}
}
}
JS 与 OC 的交互
- JS 调用OC 方法时可以发送请求,
或通过WKUIDelegate(alert 和confirm),OC 进行拦截区分请求或弹窗实现交互。- 通过WKScriptMessageHandler 参考文档
JS 调用 oc 方法
JS写法
window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
//其中<name>,就是上面方法里的第二个参数`name`。
//例如我们调用API的时候第二个参数填@"Share",那么在JS里就是:
//window.webkit.messageHandlers.Share.postMessage(<messageBody>)
//<messageBody>是一个键值对,键是body,值可以有多种类型的参数。
// 在`WKScriptMessageHandler`协议中,我们可以看到mssage是`WKScriptMessage`类型,有一个属性叫body。
// 而注释里写明了body 的类型:
Allowed types are NSNumber, NSString, NSDate, NSArray, NSDictionary, and NSNull.
//JS调用OC方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
if(message.name ==nil || [message.name isEqualToString:@""])
return;
//message body : js 传过来值
NSLog(@"message.body ==%@",message.body);
if ([message.name isEqualToString:@"loginVC"])
{
[self showLoginView];
}
else if ([message.name isEqualToString:@"gotodetailVC"])
{
//进入详情页
NSString*url = [message.body objectForKey:@"body"];
[self gotodetial:url];
}
}
oc 调用JS 方法
//OC调用JS方法,给JS传值
//加载完成后通过evaluateJavaScript实现传值
[webView evaluateJavaScript:@"document.getElementById(\"content\").offsetHeight;" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
//获取页面高度,并重置webview的frame
CGFloat documentHeight = [result doubleValue];
CGRect frame = webView.frame;
frame.size.height = documentHeight+[UIScreen mainScreen].bounds.size.height-58 /*显示不全*/;
webView.frame = frame;
}];
// NSString *js = [NSString stringWithFormat:@"isLogin(%d)",[[UserInfoSingleton shareUserInfoSingleton] isLogin]];
// [webView evaluateJavaScript:js completionHandler:^(id item, NSError * _Nullable error) {
// }];
// NSString *js1 = [NSString stringWithFormat:@"userId(\'%@\')",[UserInfoSingleton shareUserInfoSingleton].userId];
// [webView evaluateJavaScript:js1 completionHandler:^(id item, NSError * _Nullable error) {
//
// }];
- WebViewJavascriptBridge参考文档