友情提示:此篇文章只展示了核心代码。
交互方式一:原生交互(以WKWebView为例)
1. 原生调用H5方法
[wkWebView evaluateJavaScript:@"js方法名" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
if (!error) { // 成功
NSLog(@"%@",response);
} else { // 失败
NSLog(@"%@",error.localizedDescription);
}
}];
2. H5调用原生方法
1> App端:
// 1. WKWebView注入ScriptMessageHandler
[wkWebView.configuration.userContentController addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:@"xxx"];
// 2. 提供setWebViewAppearance方法,这样就能反射出H5即将传来的字符串@"setWebViewAppearance"
- (void)setWebViewAppearance {
}
2> H5端:
// 1. 获取handler
var handler = {
callHander: function (json) {
if (iOS端) {
window.webkit.messageHandlers.xxx.postMessage(JSON.stringify(json))
}
if (安卓端) {
window.xxx.postMessage(JSON.stringify(json));
}
}
// 2. 设置调用App方法所需要的传出的参数(这里是json格式)
function setAppAppearance(){
handler.callHander({
'body':"setWebViewAppearance",
'buttons': [
{
"text":"分享",
"action":""
}
],
'title':"这是webView的标题"
});
}
// 3. H5调用自己的设置方法,继而调用了原生客户端的方法
setAppAppearance();
注意:出现的xxx的位置要保持一致,这个字符串是客户端与H5约定好的,可以是任意字符串,但最好易懂为宜
交互方式二:借助第三方库WebViewJavascriptBridge交互
WebViewJavascriptBridge地址:
https://github.com/marcuswestin/WebViewJavascriptBridge
1. 原生调用H5方法
1> App端:
// 1. 初始化
WKWebViewJavascriptBridge *bridge = [WKWebViewJavascriptBridge bridgeForWebView:self.webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
}];
// 2. App callHandler是调用H5提供方法,方法名为:getShareInfo
[bridge callHandler:@"getShareInfo" handler:^(id data, WVJBResponseCallback responseCallback) {
}];
2> H5端:
// 1. 初始化
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
// 2. H5 registerHandler是给App提供方法,方法名为:getShareInfo
[bridge registerHandler:@"getShareInfo" handler:^(id data, WVJBResponseCallback responseCallback) {
}];
2. H5调用原生方法
1> App端:
// 1. 初始化
WKWebViewJavascriptBridge *bridge = [WKWebViewJavascriptBridge bridgeForWebView:self.webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
}];
// 2. App registerHandler是给H5提供方法,方法名为:showShare
[bridge registerHandler:@"showShare" handler:^(id data, WVJBResponseCallback responseCallback) {
}];
2> H5端:
// 1. 初始化
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
// 2. H5 callHandler是调用App提供方法,方法名为:showShare
[bridge callHandler:@"showShare" handler:^(id data, WVJBResponseCallback responseCallback) {
}];
另附上WKWebView详解的两篇文章,写的还是相当详细的。
- WKWebView与JS交互实战技巧之API介绍
http://blog.csdn.net/baihuaxiu123/article/details/51286109 - iOS与HTML5交互方法总结
http://blog.csdn.net/baihuaxiu123/article/details/51674726