H5喚起APP一些坑



$(function () {

function _openAppUrl(appUrl){
var ua = navigator.userAgent.toLocaleLowerCase(),
openBrowser = null,
deviceVersion = 0,
matchVersion = null,
openAppType = "",
downLoadUrl = 'http://pages.book.qq.com/pages/qidian/qddown/qdreader.htm';

//如果是在微信內部點擊的時候
if(ua.indexOf("micromessenger") != -1 ){
_openAppUrl = function(){
alert("DEMO,請在移動端的瀏覽器查看!");
}
}else{
//在瀏覽器打開,判斷是在移動端還是在PC端
if(matchVersion = navigator.userAgent.match(/OS\s*(\d+)/)){
//賦值,並且判斷
//IOS設備的瀏覽器
deviceVersion = matchVersion[1] || 0;

if(deviceVersion - 9 >= 0){
openAppType = "newType";
}
}else if(matchVersion = navigator.userAgent.match(/Android\s*(\d+)/)){
//Android的設備
deviceVersion = matchVersion[1] || 0;

if(deviceVersion - 5 >= 0){
openAppType = "newType";
}

}else{
//PC端的設備
openAppType = "pc";
}


if(openAppType == "pc"){
_openAppUrl = function(){
alert("DEMO,請在移動端的瀏覽器查看!");
}
}else if(openAppType == "newType"){
//使用新的方法,嘗試打開APP
//IOS>9,Android>5的版本
_openAppUrl = function(url){
var history = window.history,
body = $("body").eq(0),
ifr = $('<iframe class = "full-screen dn" style = "z-index:101;border:none;width:100%;height:100%;" src="'+downLoadUrl+'"></iframe>');

body.append(ifr);
$(window).on("popstate",function(e){
var state = history.state;

if(!state){
ifr.addClass("dn");
}
});

function _show(){
history.pushState({}, "下載APP鏈接頁", "");
ifr.removeClass("dn");
}

_openAppUrl = function(url){
location.href = url;
_show();
}

_openAppUrl(url);

}
}else{
//使用計算時差的方案打開APP
var checkOpen = function (cb){
var _clickTime = +(new Date()),
_count = 0,
intHandle = 0;

//啟動間隔20ms運行的定時器,並檢測累計消耗時間是否超過3000ms,超過則結束
intHandle = setInterval(function(){
_count++;
var elsTime = +(new Date()) - _clickTime;

if (_count>=100 || elsTime > 3000 ) {
clearInterval(intHandle);
//計算結束,根據不同,做不同的跳轉處理,0表示已經跳轉APP成功了
if ( elsTime > 3000 || document.hidden || document.webkitHidden) {
cb(0);
} else {
cb(1);
}

}
}, 20);
}

_openAppUrl = function(url){
var ifr = document.createElement('iframe');

ifr.src = url;
ifr.style.display = 'none';

checkOpen(function(opened){
if(opened === 1){
location.href = downLoadUrl;
}
});

document.body.appendChild(ifr);

setTimeout(function() {
document.body.removeChild(ifr);
}, 2000);
}

}

}

_openAppUrl(appUrl);

}

var appUrl = "QDReader://";

$("#tryOpenApp").on("click",function(){
_openAppUrl(appUrl);
return false;
});

});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM