0%

JS 判断手机端是否安装了某个客户端 APP

虽然在 Js 中可以启动某个 app,但是并不能判断该 app 是否安装;

但是,但是…. 还是有奇思淫巧滴,启动 app 需要的时间较长,js 中断时间长,如果没安装,js 瞬间就执行完毕。直接上代码吧!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function testApp(url) {
var timeout, t = 1000, hasApp = true;
setTimeout(function () {
if (hasApp) {
alert('安装了app');
} else {
alert('未安装app');
}
document.body.removeChild(ifr);
}, 2000)

var t1 = Date.now();
var ifr = document.createElement("iframe");
ifr.setAttribute('src', url);
ifr.setAttribute('style', 'display:none');
document.body.appendChild(ifr);
timeout = setTimeout(function () {
var t2 = Date.now();
if (!t1 || t2 - t1 < t + 100) {
hasApp = false;
}
}, t);
}
1
2
3
4
5
6
7
function isInstalled(){
var the_href=$(".down_app").attr("href");//获得下载链接
window.location="apps custom url schemes";//打开某手机上的某个app应用
setTimeout(function(){
window.location=the_href;//如果超时就跳转到app下载页
},500);
}

apps custom url schemes 是什么呢?

其实就是你与 app 约定的一个协议 URL,在 IOS 客户端或者 Android 客户端中可以设置一个 URL Scheme。例如,设置 URL Scheme:app,然后其他的程序就可以通过 URLString=app:// 调用该应用。还可以传参数,如:app://reaction/?uid=1

以上介绍了怎么创建该本地协议及调用该本地协议的方法。但这里还有个关键就是怎么判断用户是否安装了该呢?原理如下:

在手机浏览器中用 js 代码请求该协议,如果在 500ms 内,如果有应用程序能解析这个协议,那么就能打开该应用;如果超过 500ms 就跳转到 app 下载页。

欢迎关注我的其它发布渠道