WKWebView
WKWebView是现代 WebKit API 在 iOS 8 和 OS X Yosemite 应用中的核心部分。它代替了 UIKit 中的UIWebView和 AppKit 中的WebView,提供了统一的跨双平台 API。如果你的app只需要支持iOS8.0之后的版本,那么非常建议你使用WKWebView来替代UIWbeView,性能不止提升了一个档次。
坑
WKWebView与js交互
所谓轻应用,简单的来说就是原生应用集成了html页面入口,还能通过js与native实现交互,例如:通过js调用native封装好的viewController实现扫描与对应反馈功能。WKWebView初始化:
- (void)viewDidLoad {
[superviewDidLoad];
WKWebViewConfiguration*config = [[WKWebViewConfigurationalloc]init];
[config.userContentControlleraddScriptMessageHandler:self.jsBridgename:@"observe"];
self.webView= [[WKWebViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)configuration:config];
[self.viewaddSubview:self.webView];
self.jsBridge= [JSBridgeManagerjsBridgeWithDelegate:self];
self.webView.UIDelegate=self.jsBridge;
self.webView.navigationDelegate=self.jsBridge;
NSString*url = [self.pathstringByReplacingOccurrencesOfString:@" "withString:@""];
ICLog(@"webView: %@", url);
[self.webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:url]]];
}
JSBridgeManager类里进行代理设置
简单的wkwebview的代理<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
页面开始加载时调用
- (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation {
NSLog(@"%s -- %@",__FUNCTION__, webView.URL);
}
当内容开始返回时调用
- (void)webView:(WKWebView*)webView didCommitNavigation:(WKNavigation*)navigation {
NSLog(@"%s",__FUNCTION__);
}
页面加载完成之后调用
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation {
NSLog(@"%s",__FUNCTION__);
}
加载失败时调用
- (void)webView:(WKWebView*)webView didFailProvisionalNavigation:(WKNavigation*)navigation withError:(NSError*)error {
NSLog(@"%s",__FUNCTION__);
}
接收到服务器跳转请求之后调用
- (void)webView:(WKWebView*)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation*)navigation {
NSLog(@"%s",__FUNCTION__);
}
在收到响应后,决定是否跳转
- (void)webView:(WKWebView*)webView decidePolicyForNavigationResponse:(WKNavigationResponse*)navigationResponse decisionHandler:(void(^)(WKNavigationResponsePolicy))decisionHandler {
//允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
return;
}
在发送请求之前,决定是否跳转<处理js与native交互代码>
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
NSLog(@"%@", navigationAction.request.URL);
NSURL*url = navigationAction.request.URL;
if([[[urlscheme]lowercaseString]isEqualToString:kTGJSBridgeProtocolScheme]) {
[selfdispatchNotification:[urlhost]fromWebView:webView];
}else{
//forward
}
decisionHandler(WKNavigationActionPolicyAllow);
}
decidePolicyForNavigationAction能读取到由js发起的请求,native截取对应的url,读取与web端定义的协议规则,可进行通信
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSDictionary *referDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"loadUrlWithRefer"];
if (![ICTools isObjEmpty:referDict]) {
NSString *url = referDict[@"url"];
NSString *refer = referDict[@"refer"];
NSDictionary *headers = [navigationAction.request allHTTPHeaderFields];
NSString * referer = [headers objectForKey:@"Referer"];
BOOL isRightReferer = [referer isEqualToString:refer];
if (isRightReferer) {
decisionHandler(WKNavigationActionPolicyAllow);
} else {
decisionHandler(WKNavigationActionPolicyCancel);
if ([url isEqualToString:[navigationAction.request.URL absoluteString]]) {
NSMutableDictionary * muDict = [[NSMutableDictionary alloc] initWithDictionary:headers];
[muDict setObject:refer forKey:@"Referer"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [navigationAction.request URL];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
request.allHTTPHeaderFields = muDict;
[webView loadRequest:request];
});
});
}
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loadUrlWithRefer"];// 使用完成后移除
}
} else {
NSURL *url = navigationAction.request.URL;
NSString *scheme = [url scheme];
if ([scheme isEqualToString:kTGJSBridgeProtocolScheme]) {
[self dispatchNotification:[url absoluteString] fromWebView:webView];
} else {
//forward
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
[[UIApplication sharedApplication] openURL:url];
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
}