//创建一个webiview的配置项
WKWebViewConfiguration*configuretion = [[WKWebViewConfigurationalloc]init];
// Webview的偏好设置
//.WKPreferences()
//设置偏好设置
configuretion.preferences= [[WKPreferencesalloc]init];
configuretion.preferences.minimumFontSize=10;
configuretion.preferences.javaScriptEnabled=true;
configuretion.processPool= [[WKProcessPoolalloc]init];
//通过js与webview内容交互配置
configuretion.userContentController= [[WKUserContentControlleralloc]init];
[UIApplicationsharedApplication].networkActivityIndicatorVisible=YES;
//OC注册供JS调用的方法
[ configuretion.userContentControlleraddScriptMessageHandler:selfname:@"WKOcOrAndroidModel"];
//默认是不能通过JS自动打开窗口的,必须通过用户交互才能打开
configuretion.preferences.javaScriptCanOpenWindowsAutomatically=NO;
_wkwebView= [[WKWebViewalloc]initWithFrame:self.view.boundsconfiguration:configuretion];//self.view.bounds
[_wkwebViewgoBack];
[_wkwebViewgoForward];
_wkwebView.navigationDelegate=self;
_wkwebView.UIDelegate=self;
NSURL*url = [NSURLURLWithString:urlStr];//测试本地H5
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
[_wkwebViewloadRequest:request];
3.
/*
*开始加载WKWebView时调用的方法
*/
- (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation {//类似UIWebView的-webViewDidStartLoad:
NSLog(@"didStartProvisionalNavigation");
[self.viewshowLoading];
}
- (void)webView:(WKWebView*)webView didCommitNavigation:(WKNavigation*)navigation {
NSLog(@"didCommitNavigation");
}
/*
*结束加载WKWebView时调用的方法
*/
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation {//类似UIWebView的-webViewDidFinishLoad:
NSLog(@"+++++++didFinishNavigation");
[self.viewhideLoading];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if(_wkwebView.title.length>0) {
self.title=_wkwebView.title;
}
});
});
}
/*
*加载WKWebView失败时调用的方法
*/
- (void)webView:(WKWebView*)webView didFailProvisionalNavigation:(WKNavigation*)navigation withError:(NSError*)error {
//类似UIWebView的- webView:didFailLoadWithError:
NSLog(@"didFailProvisionalNavigation");
[selfloadFailedUI:@"啊哦加载失败!"];
}
- (void)webView:(WKWebView*)webView decidePolicyForNavigationResponse:(WKNavigationResponse*)navigationResponse decisionHandler:(void(^)(WKNavigationResponsePolicy))decisionHandler {
NSLog(@"decidePolicyForNavigationResponse");
// decisionHandler(WKNavigationActionPolicyAllow);
decisionHandler(WKNavigationResponsePolicyAllow);
}
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
decisionHandler(WKNavigationActionPolicyAllow);
//类似UIWebView的-webView: shouldStartLoadWithRequest: navigationType:
NSLog(@"4.%@",navigationAction.request);
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController*)userContentController
didReceiveScriptMessage:(WKScriptMessage*)message {
if([message.nameisEqualToString:@"WKOcOrAndroidModel"]) {
//打印所传过来的参数,只支持NSNumber, NSString, NSDate, NSArray,
// NSDictionary, and NSNull类型
NSLog(@"%@",message.body);
}
}
4.js的alert弹出要调用
- (void)webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(void))completionHandler {
NSLog(@"%s",__FUNCTION__);
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"alert"message:messagepreferredStyle:UIAlertControllerStyleAlert];
[alertaddAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
completionHandler();
}]];
[selfpresentViewController:alertanimated:YEScompletion:NULL];
NSLog(@"%@", message);
}
5.
iOS 8 引入WKWebView,WKWebView不支持JavaScriptCore的方式但提供message handler的方式为JavaScript与Objective-C通信.
在Objective-C中使用WKWebView的以下方法调用JavaScript:
- (void)evaluateJavaScript:(NSString *)javaScriptString
completionHandler:(void (^)(id, NSError *))completionHandler
如果JavaScript代码出错, 可以在completionHandler进行处理.
在Objective-C中注册 message handler:
// WKScriptMessageHandler protocol?
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
NSLog(@"Message: %@", message.body);
}
[userContentController addScriptMessageHandler:handler name:@"myName"];
在JavaScript将信息发给Objective-C:
// window.webkit.messageHandlers..postMessage();?
function postMyMessage()? {?
var message = { 'message' : 'Hello, World!', 'numbers' : [ 1, 2, 3 ] };?
window.webkit.messageHandlers.myName.postMessage(message);?
}