WKWebview 浅探
2017-08-04 本文已影响54人
胖红Red
1.内存:在浏览网页的时候,UIWebview 内存增长迅速,而WKWebView 相对增长缓慢。
WKWebview UIWebview WKWebview加载一段时间后内存的使用情况UIWebview加载一段时间后内存的使用情况
2. 加载速度:WKWebview 加载速度比UIWebview 更快 更流畅 这个可以自己写一个demo 试试看
3.wkwebview 的基本的创建
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.userContentController = [[WKUserContentController alloc] init];
[config.userContentController addScriptMessageHandler:self name:@"Native"];
self.wkWebView = [[WKWebView alloc] initWithFrame:frame configuration:config];
[_wkWebView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_wkWebView setNavigationDelegate:self];
[_wkWebView setUIDelegate:self];
[_wkWebView setMultipleTouchEnabled:YES];
[_wkWebView setAutoresizesSubviews:YES];
[_wkWebView.scrollView setAlwaysBounceVertical:YES];
_wkWebView.scrollView.bounces = NO;
[self addSubview:self.wkWebView];
4. 加载网页
与 UIWebview相同 代码如下
[self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
5.加载的回调 (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;
6.警告框 (WKUIDelegate协议)
web界面中有弹出警告框时调用
webView 实现该代理的webview
message 警告框中的内容
frame 主窗口
completionHandler 警告框消失调用
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(void (^)())completionHandler;
7. js 处理
在创建wkwebview 的时候,有一个js的注册方法:
[config.userContentController addScriptMessageHandler:self name:@"Native"];
然后在(WKScriptMessageHandler)里有一个方法做js的处理
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
NSLog(@"JS 调用了 %@ 方法,传回参数 %@",message.name,message.body);
}
处理js
- (void)handJS:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"Native"]) {
NSLog(@"%@",message.body);
//如果是自己定义的协议, 再截取协议中的方法和参数, 判断无误后在这里手动调用oc方法
NSDictionary *param = [self queryStringToDictionary:message.body];
NSLog(@"get param:%@",[param description]);
NSString *func = [param objectForKey:@"func"];
//调用本地函数
if([func isEqualToString:@"callFunc"]) {
......
}
}
}
而UIWebview 处理js是:
[self.webView evaluateJavaScript:javaScriptString completionHandler:completionHandler];
UIWebview 做一些事件处理的话是:
比如一个分享事件的案例:
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"shareMethod"] = ^() {
//这里可做分享事件
};
8.清除缓存
UIWebview
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURLCache * cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
[cache setDiskCapacity:0];
[cache setMemoryCapacity:0];
WKWebview
iOS9 WKWebView新方法:点击查看参考链接
NSSet *websiteDataTypes = [NSSet setWithArray:@[
WKWebsiteDataTypeDiskCache,
WKWebsiteDataTypeOfflineWebApplicationCache,
WKWebsiteDataTypeMemoryCache,
WKWebsiteDataTypeLocalStorage,
WKWebsiteDataTypeCookies,
WKWebsiteDataTypeSessionStorage,
WKWebsiteDataTypeIndexedDBDatabases,
WKWebsiteDataTypeWebSQLDatabases
]];
//你可以选择性的删除一些你需要删除的文件 or 也可以直接全部删除所有缓存的type
//NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes
modifiedSince:dateFrom completionHandler:^{
// �code
}];
注:在ios 8.0-9.0 之间可能会出现加载WKWebview 不显示的问题,可以在该VC 将要出现的时候:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
}