Jos*_*ith 24 android url-scheme ios
我们有一个处理自定义URL方案的应用程序(vstream://).当有人访问具有某些vstream://内容的网页时,如果他们没有安装我们的应用,我们需要将它们重定向到商店.
在iOS中,我们这样做:
setTimeout(function() {
window.location =
"itms://itunes.apple.com/us/app/kaon-v-stream/id378890806?mt=8&uo=4";
}, 25);
window.location = "vstream:view?code=...stuff...";
Run Code Online (Sandbox Code Playgroud)
如果window.location
分配失败,则在对话框出现之前超时会跳过App Store.(我在这里找到了这种技术:是否可以为YouTube应用程序注册基于http +域的URL方案,如YouTube和地图?)
不幸的是,这个技巧在Android中无效.我们检测到设备服务器端并写了这个而不是itms:line:
"market://details?id=com.kaon.android.vstream";
Run Code Online (Sandbox Code Playgroud)
麻烦的是,当你去一个未处理的网址方案时,iOS会抛出一个错误,Android会转到生成的页面.因此,超时永远不会有机会运行.
是否有某种方式在网页上明确测试是否处理自定义URL方案,或者有人建议像这样的黑客将在Android中工作吗?(当然,我想我需要一个黑客,无论他们使用什么浏览器都可以工作,这可能是一个很高的订单...)
更新:以下方法在Nexus 7上的Jelly Bean中不起作用.新的Chrome浏览器不会转到生成的页面(因此不需要iFrame),但似乎没有任何方法可以知道URL计划得到了处理.如果是的话,无论如何都会触发超时.如果没有处理,超时会触发.如果我使用onload处理程序和iframe,onload处理程序永远不会触发(无论是否安装了应用程序).如果我弄清楚如何知道该计划是否得到处理,我会更新...
我已经删除了我以前的解决方案中的"已解决",因为它不再起作用了.
更新2:我现在有一个很好的跨平台解决方案,适用于iOS,Android 4.1,Chrome和Android预Chrome.见下文...
更新3:谷歌再次打破了一切.看看非常好的解决方案,我已经接受了amit_saxena那里的某个地方/
Jos*_*ith 18
更新:谷歌打破了这个.请改为查看新接受的答案.
事实证明,关键是document.webkitHidden属性.将window.location设置为自定义URL方案并打开时,浏览器将继续运行,但该属性将变为false.因此,您可以对其进行测试以确定是否已处理自定义URL方案.
这是一个可以实时查看的示例
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Starting App...</title>
<script>
var URL = "kaonkaon://product.html#malvern;6";
var MARKET = "market://details?id=com.kaon.android.lepton.kaon3d";
var ITUNES = "itms://itunes.apple.com/us/app/kaon-interactive-3d-product/id525051513?mt=8&uo=4";
var QR = "http://goo.gl/gz07g"; // this should be a shortened link back to this page
function onLoad() {
if (navigator.userAgent.match(/Android/)) {
if (navigator.userAgent.match(/Chrome/)) {
// Jelly Bean with Chrome browser
setTimeout(function() {
if (!document.webkitHidden)
window.location = MARKET;
}, 1000);
window.location = URL;
} else {
// Older Android browser
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 = URL;
document.body.appendChild(iframe);
}
} else if (navigator.userAgent.match(/iPhone|iPad|iPod/)) {
// IOS
setTimeout(function() {
if (!document.webkitHidden)
window.location = ITUNES;
}, 25);
window.location = URL;
} else {
// Not mobile
var img = document.createElement("img");
img.src = "https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="+encodeURIComponent(QR);
document.body.appendChild(img);
}
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
ami*_*ena 15
下面是大多数Android浏览器的工作代码片段:
<script type="text/javascript">
var custom = "myapp://custom_url";
var alt = "http://mywebsite.com/alternate/content";
var g_intent = "intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end";
var timer;
var heartbeat;
var iframe_timer;
function clearTimers() {
clearTimeout(timer);
clearTimeout(heartbeat);
clearTimeout(iframe_timer);
}
function intervalHeartbeat() {
if (document.webkitHidden || document.hidden) {
clearTimers();
}
}
function tryIframeApproach() {
var iframe = document.createElement("iframe");
iframe.style.border = "none";
iframe.style.width = "1px";
iframe.style.height = "1px";
iframe.onload = function () {
document.location = alt;
};
iframe.src = custom;
document.body.appendChild(iframe);
}
function tryWebkitApproach() {
document.location = custom;
timer = setTimeout(function () {
document.location = alt;
}, 2500);
}
function useIntent() {
document.location = g_intent;
}
function launch_app_or_alt_url(el) {
heartbeat = setInterval(intervalHeartbeat, 200);
if (navigator.userAgent.match(/Chrome/)) {
useIntent();
} else if (navigator.userAgent.match(/Firefox/)) {
tryWebkitApproach();
iframe_timer = setTimeout(function () {
tryIframeApproach();
}, 1500);
} else {
tryIframeApproach();
}
}
$(".source_url").click(function (event) {
launch_app_or_alt_url($(this));
event.preventDefault();
});
</script>
Run Code Online (Sandbox Code Playgroud)
您需要将source_url
类添加到锚标记.
我在这里写了更多关于它的博客:
http://aawaara.com/post/88310470252/smallest-piece-of-code-thats-going-to-change-the
小智 14
这就是拯救大家的答案!
https://developers.google.com/chrome/mobile/docs/intents
<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end"> Take a QR code </a>
Run Code Online (Sandbox Code Playgroud)
如果安装了应用程序,那么您的网址将启动我,或者它会在指定的包裹上启动市场
小智 6
@jesmith,这是一个修复Android上的双重动作的干净版本.
if (navigator.appVersion.indexOf('iPhone') > -1) {
setTimeout(function noapp() { window.location="http://itunes.apple.com/app/id378890806?mt=8"; }, 25);
window.location = 'vstream:';
}
else if (navigator.userAgent.indexOf('Android') > -1) {
var iframe = document.createElement('iframe');
iframe.style.visibility = 'hidden';
iframe.src = 'vstream:';
iframe.onload = function noapp() { window.location="market://details?id=com.kaon.android.vstream"; };
document.body.appendChild(iframe);
}
Run Code Online (Sandbox Code Playgroud)