安装或者唤醒app 原生的交互
2018-06-08 本文已影响0人
陈哲_b5da
//定义&判断手机设备类型
//var u = navigator.userAgent;
//var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
//var isiOS = !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
//打开APP
function openApp() {
//生成一个url scheme,假设我们约定的scheme是myApp://type=1&id=iewo212j32这种形式的
var openIframe = createIframe();
var u = navigator.userAgent.toLowerCase();//判断手机类型
var obj = {};//交互的传值
obj.code = 1010;
obj.houseid = houseid;
obj.brandid = TenantID;
var myJSON = JSON.stringify(obj);
//console.log(myJSON);
if (/ipad|iphone|ipod/.test(u) && !window.MSStream) {
window.location.href = "dxyigeApp://" + myJSON;//与iOS交互
var loadDateTime = Date.now();
setTimeout(function () {
var timeOutDateTime = Date.now();
if (timeOutDateTime - loadDateTime < 1000) {
window.location.href = "https://itunes.apple.com/cn/app/id1373568967?mt=8";
}
}, 500);
}
else if (u.indexOf('android') > -1 || u.indexOf('adr') > -1) {
if (/chrome\/([\d\.]+)/.test(u)) {
window.location.href = "sharetest://data/" + myJSON;//与安卓交互
} else {
//抛出你的scheme
openIframe.src = "sharetest://data/" + myJSON;
}
setTimeout(function () {
window.location.href = "http://www.yigeroom.com/app/yige.apk";
}, 500);
}
else {
openIframe.src = "sharetest://data/" + myJSON;
setTimeout(function () {
window.location.href = "http://www.yigeroom.com/app/yige.apk";
}, 500);
}
}
//点击下载 ,如果已安装就在浏览器换气app
$(".download a").on("click", function (e) {
openApp();
})
//交互定写的模块
// Javascript 语言
// 通知 iPhone UIWebView 加载 url 对应的资源
// url 的格式为: gap:something
function loadURL(url) {
var iFrame;
iFrame = document.createElement("iframe");
iFrame.setAttribute("src", url);
iFrame.setAttribute("style", "display:none;");
iFrame.setAttribute("height", "0px");
iFrame.setAttribute("width", "0px");
iFrame.setAttribute("frameborder", "0");
document.body.appendChild(iFrame);
// 发起请求后这个iFrame就没用了,所以把它从dom上移除掉
iFrame.parentNode.removeChild(iFrame);
iFrame = null;
}