有没有办法使用JavaScript检查手机上当前是否已注册URL方案?
有没有办法使用JavaScript检查手机上当前是否已注册URL方案?
不是无缝的。 但是有一种类似于检查弹出窗口是否被阻止的方法。
当您尝试一个不被支持的URL scheme时,Safari会警告用户它不知道如何处理它,并停留在同一页上。
因此,如果您给您的应用程序调用一些时间来激活,比如300毫秒,然后做一些其他事情来响应scheme的不存在。
这不是最美观的方法,但它可以工作:
function startIThrown(){
document.location = 'ithrown://restart';
setTimeout(function(){
if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
}
}, 300);
}
<a href="#" onclick="startIThrown()">Restart iThrown</a>
这里有一个解决方案,当你从应用程序回来时不会显示弹出窗口,它假定你离开时间超过400毫秒:
function startiThrown() {
document.location = appurl;
var time = (new Date()).getTime();
setTimeout(function(){
var now = (new Date()).getTime();
if((now - time)<400) {
if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
}
}
}, 300);
}
我发现pagehide
事件比依赖系统时间更加稳定。对于那些喜欢非jQuery方式的人,这里是代码片段。
var appurl = 'custom://url';
var appstore = 'https://itunes.apple.com/us/app/your-app';
var timeout;
function preventPopup() {
clearTimeout(timeout);
timeout = null;
window.removeEventListener('pagehide', preventPopup);
}
function startApp() {
window.location = appurl;
timeout = setTimeout(function(){
if(confirm('You do not seem to have the App installed, do you want to go download it now?')){
document.location = appstore;
}
}, 1000);
window.addEventListener('pagehide', preventPopup);
}
<a class="uri-link" data-uri="qobuzapp://" href="#">URI</a>
Javascript(这里使用jQuery):
var windowHasFocus;
$(window).focus(function() {
windowHasFocus = true;
}).blur(function() {
windowHasFocus = false;
});
function goToUri(uri) {
window.location = uri;
setTimeout(function(){
if (windowHasFocus) {
if (confirm('You do not seem to have Qobuz installed, do you want to go download it now?')){
window.location = 'http://www.qobuz.com';
}
}
}, 100);
}
$('a').on('click', function(){
goToUri($(this).data('uri'));
});
iOS 6.0起,苹果推出了智能应用横幅(Smart App Banners),这是我们大多数人所需要的:
请包含以下元标记:
<meta name="apple-itunes-app" content="app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL">
taken from here: Safari Web Content Guide
这里是前两个解决方案的变体。它将创建一个可以在Google Chrome中打开的链接。如果失败,它将使用http打开链接。
<script>
function checkChrome(h){
document.location=h;
var time = (new Date()).getTime();
setTimeout(function(){
var now = (new Date()).getTime();
if((now-time)<400) {
if(confirm('Missing Chrome. Download it now?')){
document.location = 'http://itunes.apple.com/us/app/chrome/id535886823?mt=8';
} else {
document.location=h.replace('googlechrome','http');
}
}
}, 300);
}
</script>
<a href="googlechrome://www.google.com" onclick="checkChrome(this.href);return false;">Open Google with Chrome</a>
function startiThrown() {
document.location = appurl;
var time = (new Date()).getTime();
setTimeout(function(){
setTimeout(function(){ // <-- start new thread after activation
var now = (new Date()).getTime();
if((now - time)<400) {
if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
}
}
}, 10); // <-- start new thread after activation
}, 300);
}
我尝试只使用“pagehide”事件,但在Firefox中无法工作。我创建了这个版本http://jsfiddle.net/thiagomata/6tvoc4f1/2/,可以在Firefox、Google Chrome和Safari中运行。我还没有在Internet Explorer中进行测试。
为了使它在Firefox中工作,有一件必要的事情是使用Iframe来设置src。这使我能够调用应用程序而不离开我的页面。
<a class="uri-link" href="#"
data-uri-app="myapp://"
data-url-app-not-found="http://www.google.com?q=not-found-link"
>
Example 1
</a>
<a class="uri-link" href="#"
data-uri-app="myapp://"
data-url-app-not-found="http://www.google.com?q=not-found-link"
data-url-app-found="http://www.google.com?q=found-link"
>
Example 2
</a>
<a class="uri-link" href="#"
data-uri-app="notexists://"
data-url-app-not-found="http://www.google.com?q=not-exists"
>
Example 3
</a>
<iframe id="callapp" style="display:none"></iframe>