About APP function H5 wake implementation (reprint)

April 26, 2017 18:27:54  to read the number 24188 

 Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/sinat_29194935/article/details/70820398

      First of all, I'm a back-end, write java, and not even engage in the mobile side, so js this area a little foundation, but not professional, wrong to appear please forgive me, the original project requires a H5 page opens APP function forcibly to do , no way to just try and do some online tutorials are basically almost routine, APP manufacturers did not provide start-interface and the like, can only consider starting the application from the system registry, so this code below is based on you know this app is scheme_url and scheme_hosts what is the basis. Who does not know these two addresses can only say: degree of your mother find you.

      Does not necessarily guarantee hundred percent open, only to set the timer to start applications, if belated reaction of the terminal, six seconds after launch yet, the code did not consider the application, it will jump to the download page to download. Of course, a little bit easier Andrews, IOS is more closed. What then do not say, on the first js code

//启动app方法
function startApp(url, url2) { //url是跳转的scheme地址,这个建议下个反编译的软件,去第三方apk查他们设置的scheme_url和scheme_host是什么,我就是这样干的 //url2是应用下载地址,要分清ios和android的不一样 //将下载地址保存到全局变量 downloadUrl = url2; if (ua.match(/ipad|iphone|ipod|ios/i)) { //外部一个定时器,专门盯着启动app的定时器loop;就叫它killer吧 //计时6秒,之后干掉loop. window.setTimeout(function() { $('#message').html(''); $('.result-message').eq(0).css("display", "none"); clearTimeout(loop); time = parseInt('6000') / 1000; }, 6000); //尝试启动应用 location.href = url; //同时开始应用启动倒计时 countDown(); } else { //安卓的就是用iframe来测试是否安装和启动应用了 window.setTimeout(function() { $('#message').html(''); $('.result-message').eq(0).css("display", "none"); clearTimeout(loop); time = parseInt('6000') / 1000; }, 6000); //创建iframe并启动应用入口 openApp(url); } } function openApp(src) { // 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP var ifr = document.createElement('iframe'); ifr.src = src; ifr.style.display = 'none'; document.body.appendChild(ifr); //切换到iframe时 //此时,会有个问题,如后切换到应用时间小于killer所需要杀死loop的时间,loop就会跳到下载提示,killer`就失去作用了 countDown(); window.setTimeout(function() { document.body.removeChild(ifr); }, 5000); //倒计时 } function countDown() { //每秒调用一次 loop = window.setTimeout('countDown()', 1000); if (time > 0) { $('.result-message').eq(0).css("display", "block"); $('#message').html('正在尝试打开客户端 ' + time + 's'); time--; if (time == 0) { /*if(ua.match(/ipad|iphone|ipod|ios/i)){ console.log(downloadUrl); location.href = downloadUrl; }*/ //如果计时到0,loop任然没被干掉,就说明应用没有启动,此时,跳到下载提示界面 //定时器的局限性还是很大,不能响应式反应,所以只能做到这一步了 $('#message').html(''); $('.result-message').eq(0).css("display", "none"); clearTimeout(loop); time = parseInt('6000') / 1000; var btnArray = ['否', '是']; mui.confirm('您没有安装该应用,是否下载安装包?', '应用下载', btnArray, function(e) { if (e.index == 1) { location.replace(downloadUrl); } else { return; } }); } } } //url2是应用下载地址,要分清ios和android的不一样 //将下载地址保存到全局变量 downloadUrl = url2; if (ua.match(/ipad|iphone|ipod|ios/i)) { //外部一个定时器,专门盯着启动app的定时器loop;就叫它killer吧 //计时6秒,之后干掉loop. window.setTimeout(function() { $('#message').html(''); $('.result-message').eq(0).css("display", "none"); clearTimeout(loop); time = parseInt('6000') / 1000; }, 6000); //尝试启动应用 location.href = url; //同时开始应用启动倒计时 countDown(); } else { //安卓的就是用iframe来测试是否安装和启动应用了 window.setTimeout(function() { $('#message').html(''); $('.result-message').eq(0).css("display", "none"); clearTimeout(loop); time = parseInt('6000') / 1000; }, 6000); //创建iframe并启动应用入口 openApp(url); } } function openApp(src) { // 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP var ifr = document.createElement('iframe'); ifr.src = src; ifr.style.display = 'none'; document.body.appendChild(ifr); //切换到iframe时 //此时,会有个问题,如后切换到应用时间小于killer所需要杀死loop的时间,loop就会跳到下载提示,killer`就失去作用了 countDown(); window.setTimeout(function() { document.body.removeChild(ifr); }, 5000); //倒计时 } function countDown() { //每秒调用一次 loop = window.setTimeout('countDown()', 1000); if (time > 0) { $('.result-message').eq(0).css("display", "block"); $('#message').html('正在尝试打开客户端 ' + time + 's'); time--; if (time == 0) { /*if(ua.match(/ipad|iphone|ipod|ios/i)){ console.log(downloadUrl); location.href = downloadUrl; }*/ //如果计时到0,loop任然没被干掉,就说明应用没有启动,此时,跳到下载提示界面 //定时器的局限性还是很大,不能响应式反应,所以只能做到这一步了 $('#message').html(''); $('.result-message').eq(0).css("display", "none"); clearTimeout(loop); time = parseInt('6000') / 1000; var btnArray = ['否', '是']; mui.confirm('您没有安装该应用,是否下载安装包?', '应用下载', btnArray, function(e) { if (e.index == 1) { location.replace(downloadUrl); } else { return; } }); } } }

Guess you like

Origin www.cnblogs.com/wang-sai-sai/p/11038150.html