雖然在Js中可以啟動某個app,但是並不能判斷該app是否安裝;
但是,但是....還是有奇思淫巧滴,啟動app需要的時間較長,js中斷時間長,如果沒安裝,js瞬間就執行完畢。直接上代碼吧!
一、
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); }
二、
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
以上介紹了怎么創建該本地協議及調用該本地協議的方法。但這里還有個關鍵就是怎么判斷用戶是否安裝了該app呢?原理如下:
在手機瀏覽器中用js代碼請求該協議,如果在500ms內,如果有應用程序能解析這個協議,那么就能打開該應用;如果超過500ms就跳轉到app下載頁。