手机浏览器调用app


js 调用
---------------------------------------
/**
 * 移动浏览器上app打开及下载
 * yancey.zhang
 * 20150105
 */
var loadIframe = null;
var ios_schemes = "wx737d86c854364366://sinokorealife/openwith?";
var android_schemes = "wx737d86c854364366://sinokorealife/openwith?";
var ios_appstore_url = "http://fir.im/rjuy";
var android_appstore_url = "http://fir.im/q6ct";
var IS_IPAD = navigator.userAgent.match(/iPad/i) != null, 
IS_IPHONE = !IS_IPAD && ((navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null)), 
IS_IOS = IS_IPAD || IS_IPHONE, 
IS_ANDROID = !IS_IOS && navigator.userAgent.match(/android/i) != null, 
IS_MOBILE = IS_IOS || IS_ANDROID;
var isChrome =navigator.userAgent.match(/Chrome\/([\d.]+)/) || navigator.userAgent.match(/CriOS\/([\d.]+)/);
var isWeixin =navigator.userAgent.match(/MicroMessenger/i);
var timeout=500;
//创建iframe ,防止有些浏览器再找不到app的时候报错
function createIframe() {
var iframe = document.createElement("iframe");
iframe.style.cssText = "display:none;width:0px;height:0px;";
document.body.appendChild(iframe);
loadIframe = iframe;
}
var winHeight = $(window).height();
//安卓系统中针对谷歌chrome浏览器的调用方式
function getChromeIntent(param){
var url="intent://sinokorealife/openwith?"+param+"/#Intent;scheme=wx737d86c854364366;package=com.sinokorealife.oa;end";
    return  url;
}
//main 打开APP
function openApp(param) {
if (IS_IOS) {
loadIframe.src = ios_schemes + param;
var t = Date.now();
setTimeout(function() {
if (Date.now() - t < timeout+100) {
location.href = ios_appstore_url;
}else{
window.close();
}
}, timeout);
} else if (IS_ANDROID) {
//alert(navigator.userAgent);
if(isChrome){
location.href=getChromeIntent(param);
}else{
location.href =android_schemes+ param;
}
var t = Date.now();
setTimeout(function() {
if (Date.now() - t < timeout+100) {
location.href = android_appstore_url;
}else{
window.close();
}
}, timeout);
} else{
alert("暂不支持当前系统!");
}
}
createIframe();


ps:目前这种方式支持IOS,和安卓系统中的不同浏览器,QQ浏览器,chrome,和系统自带都可以打开。
ps: 这个代码需求是要求微信的wap页面能够调用企业app,目前只能提示用户用浏览器打开,才能下载,微信中不能直接打开,但是有一种方式是微信提供的跳转,跳转方式:http://mp.weixin.qq.com/mp/redirect?url=你的跳转地址#weixin.qq.com#wechat_redirect

这种方式目前IOS可以打开,安卓的不行,还有就是,IOS会出现有时候能打开,有时候不行,还在研究中。。。

猜你喜欢

转载自yancel.iteye.com/blog/2174916