H5唤醒移动端APP,或跳转到App Store和应用宝

//实际上就是新建一个iframe的生成器
var  createIframe=(function(){
    var iframe;
    return function(){
        if(iframe){
            return iframe;
        }else{
            iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            return iframe;
        }
    }
})()
var openApp=function(){
    // 移动端配置的schema,如要传参,按正常的url传参方式拼接到路径后面即可
    // tiaoyangtaiyang为移动端的schema,com.sunpro为移动端的host
    var localUrl="taiyangtaiyang://com.sunpro";
    var openIframe=createIframe();
    var u = navigator.userAgent;
    var isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    var isAndroid= u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
    // 判断是否为微信端打开,如果是,则隐藏页面所有东西,展示请用浏览器打开的图片
    if(isWeiXin()){
        $("body div").hide();
        if(isIos){
            $("#sharePhoto").find("img").attr("src","../images/shareIOS.jpg");
        }else{
            $("#sharePhoto").find("img").attr("src","../images/share.jpg");
        }
        $("#sharePhoto").show();
        $('html,body').css("overflow","hidden");
        return false;
    }else{
        $("#sharePhoto").hide();
    }
    if(isIos){
        // 根据时间来判断移动端是否装有app,如果有跳转到和移动端约定好的schema,如果没有装有app则跳转到App Store
       // 也可以直接跳转到App Store,从appstore打开或者下载app
        var loadDateTime = Date.now();
        window.location.href = localUrl;
        setTimeout(function () {
            var timeOutDateTime = Date.now();
            if (timeOutDateTime - loadDateTime < 1000) {
                window.location.href = "https://itunes.apple.com/cn/app/[name]/id[id]";
            }
        }, 500);
    }else if(isAndroid){
        //判断是否是android,具体的判断函数自行百度
        if (isChrome) {
            //chrome浏览器用iframe打不开得直接去打开,算一个坑
            window.location.href = localUrl;
        } else {
            //抛出你的scheme
            openIframe.src = localUrl;
        }
        setTimeout(function () {
            window.location.href ="http://a.app.qq.com/o/simple.jsp?pkgname=[name]";          /* http://t.cn/RcxMVvL*/
        }, 500);
    }else{
        //主要是给winphone的用户准备的,实际都没测过,现在winphone不好找啊
        openIframe.src = localUrl;
        setTimeout(function () {
            window.location.href = "你的下载地址";
        }, 500);
    }
}
/*判断是否是ios9以上*/
function isIOS9() {
    //获取固件版本
    var getOsv = function () {
        var reg = /OS ((\d+_?){2,3})\s/;
        if (navigator.userAgent.match(/iPad/i) || navigator.platform.match(/iPad/i) || navigator.userAgent.match(/iP(hone|od)/i) || navigator.platform.match(/iP(hone|od)/i)) {
            var osv = reg.exec(navigator.userAgent);
            if (osv.length > 0) {
                return osv[0].replace('OS', '').replace('os', '').replace(/\s+/g, '').replace(/_/g, '.');
            }
        }
        return '';
    };
    var osv = getOsv();
    var osvArr = osv.split('.');
    //初始化显示ios9引导
    if (osvArr && osvArr.length > 0) {
        if (parseInt(osvArr[0]) >= 9) {
            return true
        }
    }
    return false
}
function isWeiXin(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }else{
        return false;
    }
}

你可能感兴趣的:(H5)