<!doctype html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no;" name="viewport" />
<script>
// 检查是否安装app
function openApp(e){
var u = window.navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
if(isAndroid){
android();
}
if(isiOS){
ios();
}
}
function android(){
var _clickTime = new Date().getTime();
window.location.href = '??????????????????'; /***打开app的协议,有安卓同事提供***/
//启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束
var _count = 0, intHandle;
intHandle = setInterval(function(){
_count++;
var elsTime = new Date().getTime() - _clickTime;
if (_count>=100 || elsTime > 3000 ) {
console.log(_count)
console.log(elsTime)
clearInterval(intHandle);
//检查app是否打开
if ( document.hidden || document.webkitHidden) {
// 打开了
window.close();
} else {
// 没打开
alert('没打开')
// window.location.href = "??????????????";//下载链接
}
}
}, 20);
}
function ios(){
var _clickTime = +(new Date());
var ifr = document.createElement("iframe");
ifr.src = "??????????????"; /***打开app的协议,有ios同事提供***/
ifr.style.display = "none";
document.body.appendChild(ifr);
//启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束
var _count = 0, intHandle;
intHandle = setInterval(function(){
_count++;
var elsTime = +(new Date()) - _clickTime;
console.log(_count,elsTime,+(new Date()),_clickTime)
if (_count>=100 || elsTime > 3000 ) {
clearInterval(intHandle);
document.body.removeChild(ifr);
//检查app是否打开
if ( document.hidden || document.webkitHidden) {
// 打开了
window.close();
} else {
// 没打开
alert('没打开')
// window.location.href = "???????????????";//下载链接
}
}
},20);
}
var objbtn=document.getElementById('btn');
objbtn.onclick=function(){
openApp()
}
</script>
</head>
<body style="background-color: #fff;" >
<!--测试app调起-->
<div id="btn" style="font-size: 40px;background: blue;color: #fff;height: 56px;text-align: center;">btn</div>
<!--测试app调起over-->
</body>
</html>
It is important to install the app to open the app, but does not allow users to click OK at the time, and jump to the download page.
Users did not install the app, and can not wait too long, not to the app download page.
If open app successfully url scheme, then the current h5 will enter the background, there will be significantly delayed by a timer. Use of time is determined.
- Because Android phones, page into the background, timer setTimeout will run constantly, so here use setInterval, smaller intervals repeated many times. It is determined according to the accumulated time.
- document.hidden good support for greater than 4.4webview, a page visibility api.
ios
window.location.href='rtjr://app.rongtuojinrong.com?flag=23&pid={$pid}';
js such an agreement can not be transferred from the url
<a href="rtjr://app.rongtuojinrong.com?flag=23&pid={$pid}" ></a>
Only with a label, or iframe
Reproduced in: https: //www.cnblogs.com/gaidalou/p/11058484.html