h5与安卓和ios交互 我会带着你远行 2022-04-22 15:50 251阅读 0赞 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>原生与H5交互</title> <script> window.function(){ var btn = document.getElementsByTagName('button')[0]; btn.onclick=function(){ alert(navigator.userAgent) //H5调取原生方法,建立连接,并向原生传参 if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //做一下约定,ios那边去检测一下 //window.getPic(); try { window.webkit.messageHandlers.getMessage.postMessage(jsonStr) } catch(error) { console.log(error) } }else{ window.my.getPic(); } } //原生调取H5方法,安卓和ios都可以用这一种方法 function showPic (str) { //str则为原生传过来的数据 } } function getMessage(json){ //空方法 } </script> </head> <body> <button >跳转到原生页面</button> </body> </html>
还没有评论,来说两句吧...