最近在App中需要做一些页面,其中涉及到了与原生APP交互的问题,现在做下总结方便以后查询
Android与JS交互
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <script> var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { trident: u.indexOf('Trident') > -1, presto: u.indexOf('Presto') > -1, webKit: u.indexOf('AppleWebKit') > -1, gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, iPad: u.indexOf('iPad') > -1, webApp: u.indexOf('Safari') == -1 }; }(), language:(navigator.browserLanguage || navigator.language).toLowerCase() } function showToast() { android.showToast("'title: Money+Funny,这可能是全成都最适合你的兼职!','content:拼K高校首席体验官公开招募!底薪1000元多玩多赚!速来!速来!速来!','urlId:http://www.tanshikeji.com/Home/Active/special_form.html'");
} function showClick(e) { if( navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1){
} android.showClick("http://www.tanshikeji.com/Home/Active/special_form.html"); } document.getElementById('android_share').onclick = showToast; document.getElementById('from_click').onclick = showClick; </script>
|
IOS与JS交互:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <script>
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 = 'wvjbscheme://__BRIDGE_LOADED__'; document.documentElement.appendChild(WVJBIframe); setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0) }
setupWebViewJavascriptBridge(function(bridge) { document.getElementById('ios_share').onclick = function (e) { bridge.callHandler('getShareObjC', { 'title': 'Money+Funny,这可能是全成都最适合你的兼职!', 'content':'拼K高校首席体验官公开招募!底薪1000元多玩多赚!速来!速来!速来!', 'urlId':'http://www.tanshikeji.com/Home/Active/special_form.html' }, function(response) {
}) } }) </script>
|