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>
#pragma mark - WKNavigationDelegate
页面开始加载时调用
- (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);
}
处理js发送的请求,如果以某个jsbridge为scheme的则为交互信息
- (void)dispatchNotification:(NSString*)notificationString
fromWebView:(WKWebView*)webView
{
if([notificationStringhasPrefix:kTGJSBridgePostNotificationWithId]) {
NSDictionary*responseDict = [selffetchNotificationWithStr:notificationStringfromWebView:webView];
if([self.delegaterespondsToSelector:@selector(jsBridge:didReceivedNotificationDictionary:fromWebView:)]) {
[self.delegatejsBridge:selfdidReceivedNotificationDictionary:responseDictfromWebView:webView];
}
}else{
}
}
decidePolicyForNavigationAction能读取到由js发起的请求,native截取对应的url,读取与web端定义的协议规则,可进行简单的通讯。