2014-01-18 114 views

回答

9

请参阅Maximilian Hoffmann's answer了解更强大的解决方案。


这种方法很常见 - 劫持超时重定向到一个不同的URL。这种方法适合你吗?

<a id="applink" href="comgooglemaps://?q=Red+Lobster+Billings">Show map</a> 

<script type="text/javascript"> 

var backup = "http://maps.google.com/?q=Red+Lobster+Billings"; 

function applink(fail){ 
    return function() { 
     var clickedAt = +new Date; 
     setTimeout(function(){ 
      if (+new Date - clickedAt < 2000){ 
       window.location = fail; 
      } 
     }, 500);  
    }; 
} 

document.getElementById("applink").onclick = applink(backup); 

</script> 
+0

This如果我无法完成我所想的内容,可能会很有用,但我真正希望能够做的是确定链接是否要在用户之前启动应用程序(AKA是否安装该应用程序)点击链接。我想在页面加载执行一些JavaScript可能可以为此工作。有什么想法吗?我希望我已经清楚了...... – novicePrgrmr

+0

没有接口询问链接是否可以工作......只能尝试它并处理结果(不幸)。您可以在bugreport.apple.com上提交移动Safari增强请求,但我想这可能会打开许多​​隐私问题,这就是Apple不允许这样做的原因。 –

+0

这个解决方案是否会阻止超时弹出?如果是,那么这是正确的解决方案。 – novicePrgrmr

0

没有让你知道,如果一个链接将工作的方式,但有Safari浏览器利用所谓的Smart App Banners

enter image description here

<!DOCTYPE html> 
<html> 
<head> 
<meta name="Google Maps" content="app-id=585027354"/> 
</head> 

<body> 
The content of the document...... 
</body> 

</html> 

什么,它基本上是被检查是否安装了一个应用程序。如果未安装,用户将被引导至应用商店。如果已安装,用户将能够使用通常使用url方案传递的相关数据从网站打开应用程序。 你可以使用如果谷歌地图。

这样做的缺点是它只能在Safari上工作,但它仍然比没有好。

4

解决方案是在您的页面上添加一个iframe的URL方案。如果应用程序未安装,它会静默失败,因此如果打开应用程序,您需要通过计时器检查是否工作。

// detect iOS 
if (['iPhone', 'iPad'].indexOf(navigator.platform) > -1) { 

    // create iframe with an Apple URL scheme 
    var iframe = document.createElement('iframe'); 
    iframe.src = 'twitter://'; 

    // hide iframe visually 
    iframe.width = 0; 
    iframe.height = 0; 
    iframe.frameBorder = 0; 

    // get timestamp before trying to open the app 
    var beforeSwitch = Date.now(); 

    // schedule check if app was opened 
    setTimeout(function() { 
    // if this is called after less than 30ms 
    if (Date.now() - beforeSwitch < 30) { 

     // do something as a fallback 

    } 
    }); 

    // add iframe to trigger opening the app 
    document.body.appendChild(iframe); 
    // directly remove it again 
    iframe.parentNode.removeChild(iframe); 
} 

我写了一个使用这种方法,如果安装打开iOS上的Twitter的应用程序一个post with a more detailed example

+0

这比我的答案要好。我更新了我的答案,以链接到这一个。 –