WKWebView是支持ios8以后的,用起来比 uiwebview方便,而且内存占用率少
pragma mark – 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 didFailNavigation:(WKNavigation )navigation withError:(NSError *)error {
}
//接收到服务器 跳转请求之后调用,即从一个h5 进入另一个h5
- (void)webView:(WKWebView )webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation )navigation {
}
//在收到响应之前,决定是否跳转
- (void)webView:(WKWebView )webView decidePolicyForNavigationAction:(WKNavigationAction )navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
decisionHandler(WKNavigationActionPolicyAllow);
}
//在发送请求之前 ,决定是否跳转
- (void)webView:(WKWebView )webView decidePolicyForNavigationResponse:(WKNavigationResponse )navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
decisionHandler(WKNavigationResponsePolicyAllow);
}
上边的都比较简单,下边讲一下js 调用oc的方法
js中 window.alert(‘找到’+count+’张图’)
则会调用
- (void)webView:(WKWebView )webView runJavaScriptAlertPanelWithMessage:(NSString )message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
NSLog(@”alert %@”,message);
completionHandler();
}
window.confirm(‘找到’+count+’张图’)
会调用
- (void)webView:(WKWebView )webView runJavaScriptConfirmPanelWithMessage:(NSString )message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@”confirm %@”,message);
completionHandler(YES);//yes 为确认,no 为取消
}
js 中的 window.confirm(‘找到’+count+’张图’)会调用
- (void)webView:(WKWebView )webView runJavaScriptTextInputPanelWithPrompt:(NSString )prompt defaultText:(NSString )defaultText initiatedByFrame:(WKFrameInfo )frame completionHandler:(void (^)(NSString * str))completionHandler {
//输入 文字,并通过completionhandler带回去
}
如果是js自己的命名的方法,则需要统一一下
NSString * jsMessage = @”window.webkit.messageHandlers.flut.postMessage(‘123456’)”;
其中flut是和h5定义的名字, 123456则是h5 想要传给你的值,可以是对象,数组,字典均可。
pragma mark – WKScriptMessageHandler
(void)userContentController:(WKUserContentController )userContentController didReceiveScriptMessage:(WKScriptMessage )message {
NSLog(@”message.name %@ message.body %@”,message.name,message.body);
}
在ios中则会调用上边的方法,通过解析之后再调用
// [self.webView evaluateJavaScript:confirmjs completionHandler:^(id object, NSError * _Nullable error) {
// }];类似的js方法,即可实现和h5的交互
网友评论