天天看点

html按钮打开某些apps,如果没有则打开应用商店时ios中遇到的问题

在html中打开手机中的app,可以使用连接到地址例如:

href = "myapp://custom_url";这样去打开

在android中,可以设置一个timeout去执行判断是否打开了应用,如果没打开则跳转到market这样的一个方式.

setTimeout(function() {
                if (!document.webkitHidden)
                    window.location = "market://***"
            }, 1000);
           

但是在ios中使用setTimeout却不会停止,那么解决方法来了.

借用:(感谢各位巨人,站在他们的肩膀上)

http://stackoverflow.com/questions/7231085/how-to-fall-back-to-marketplace-when-android-custom-url-scheme-not-handled?rq=1

也就是先判断一下 webkitHidden(浏览器是不是可见),如果打开了app则浏览器会被切到后边,就会终止掉setTimeout后面的操作,否则就打开了market

贴上代码:

function openApps() {
    if (navigator.userAgent.match(/Android/)) {//android
        if (navigator.userAgent.match(/Chrome/)) {//chrome
            setTimeout(function() {
                if (!document.webkitHidden)
                    window.location = "market://***";//打开市场
            }, 1000);
            window.location = "myapp://custom_url";//打开自己的应用
        } else {
            // 通用的方式
            var iframe = document.createElement("iframe");
            iframe.style.border = "none";
            iframe.style.width = "1px";
            iframe.style.height = "1px";
            var t = setTimeout(function() {
                window.location = "market://***";//打开市场
            }, 1000);
            iframe.onload = function () { 
			clearTimeout(t) 
			};
            iframe.src = "myapp://custom_url";//打开自己的应用
            document.body.appendChild(iframe);
        }
     } else if (navigator.userAgent.match(/iPhone|iPad|iPod/)) {// IOS
         setTimeout(function() {
             if (!document.webkitHidden)//判断是不是可见
                 window.location = "itms://itunes.apple.com/****";//到苹果的商店...
         }, 1000);

         window.location = "myapp://custom_url";//打开自己的应用

     } else {//非手机活pad端
        alert("请在手机端打开");//或者弄个其他连接什么的
     }
}
           

好了,就是这些了,感谢stackoverflow的各位巨人.