首先需要app进行Schema注册,具体怎么弄,这里就不说了。好弄。
原理是当通过schema方式打开客户端后,手机当前聚焦的视图就不是浏览器了,浏览器就暂时不会执行setTimeout和setInterval方法
下面以腾讯微博为例
function redirect(){
location.href="TencentWeibo://xxx";
var t = Date.now();
setTimeout(function(){
// 之所以加个100 是因为settimeout不是那么准时
if(Date.now()-t < 600){
location.href="t.qq"
}
},500)
}
如果直接应用上诉代码,会发现在android和ios部分浏览器中,如果用户未安装app,会直接跳到报错页面。所以我采用iframe的方式。通过iframe方式启动app
var loadIframe = null;
function createIframe(){
var iframe = document.createElement("iframe");
iframe.style.cssText = "display:none;width:0px;height:0px;";
document.body.appendChild(iframe);
loadIframe = iframe;
}
function redirect(){
loadIframe.src="TencentWeibo://xxx";
var t = Date.now();
setTimeout(function(){
if(Date.now()-t < 600){
location.href="t.qq"
}
},500)
}
createIframe();
这种写法在大部分浏览器中是没问题的,不过还有以下问题:
1、在ios低版本的safari上会提示一个未找到页面的提示,然后执行setTimeout方法。
2、在Android的chrome上没效果
第一个bug暂时没有解决,如果有同学碰到的话,告诉俺一下哈。产品追着我改这个bug呢 第二个需要去查看下chrome的资料,参考:(Android Intents with Chrome)
修改写法如下:
/**
* 移动浏览器上app下载
*
*/
;
var mobileAppInstall = (function(){
var ua = navigator.userAgent,
loadIframe,
win = window;
function getIntentIframe(){
if(!loadIframe){
var iframe = document.createElement("iframe");
iframe.style.cssText = "display:none;width:0px;height:0px;";
document.body.appendChild(iframe);
loadIframe = iframe;
}
return loadIframe;
}
function getChromeIntent(url){
// 根据自己的产品修改吧
return "intent://t.qq/#Intent;scheme="+url+";packaget.WBlog;end";
}
var appInstall = {
isChrome:ua.match(/Chrome/([d.]+)/) || ua.match(/CriOS/([d.]+)/),
isAndroid:ua.match(/(Android);?[s/]+([d.]+)?/),
timeout:500,
/**
* 尝试跳转appurl,如果跳转失败,进入h5url
* @param {Object} appurl 应用地址
* @param {Object} h5url http地址
*/
open:function(appurl,h5url){
var t = Date.now();
appInstall.openApp(appurl);
setTimeout(function(){
if(Date.now() - t < appInstall.timeout+100){
h5url && appInstall.openH5(h5url);
}
},appInstall.timeout)
},
openApp:function(appurl){
if(appInstall.isChrome){
if(appInstall.isAndroid){
win.location.href = getChromeIntent(appurl);
}else{
win.location.href = appurl;
}
}else{
getIntentIframe().src = appurl;
}
},
openH5:function(h5url){
win.location.href = h5url;
}
}
return appInstall;
})();
ios 6.0 以上app推荐
<meta name="apple-itunes-app" content="app-id=432274380">
在微信中如果想通过Schema方式打开应用,貌似除腾讯以外的是不行了。
// 判断是否安装
WeixinJSBridge.invoke("getInstallState", {packageName: t.WBlog",packageUrl: "TencentWeibo://"}, function(e) {
var n = e.err_msg;
if(n.indexOf("get_install_state:yes") > -1){
alert("已经安装");
}
})
// 安装
WeixinJSBridge.invoke("addDownloadTask", {task_name: "腾讯微博",task_url: "softfile.3g.qq/msoft/180/2104/2104/WBlog_4.2.1_100000024_131210163458a.apk",file_md5: "31C49560B20057ECBB4C7C52D35B00DB"}, function(t) {
})
本文发布于:2025-04-05 09:06:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1743815210584033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |