如何防止尝试打开未安装的原生应用程序的iOS Safari浏览器警报?

我一直在寻找一种方法来从浏览器中打开一个本地的iOS应用程序。 我在这里find了一个不错的解决scheme: 是否可以为YouTube和Google地图等iPhone应用程序注册基于http +域名的URL Scheme?

这个解决scheme在安装应用程序时效果很好。 但是当用户没有安装这个应用程序 – Safari浏览器会popup一个错误消息,指出“Safari无法打开该页面,因为地址无效”。

有没有办法来防止这种行为,而是提示用户下载应用程序?

这是一个适合我的解决scheme:

var timeout; function preventPopup() { clearTimeout(timeout); timeout = null; window.removeEventListener('pagehide', preventPopup); } function openApp() { $('<iframe />') .attr('src', appurl) .attr('style', 'display:none;') .appendTo('body'); timeout = setTimeout(function() { document.location = appstore; }, 500); window.addEventListener('pagehide', preventPopup); } 

使用iframe。

 $('<iframe />').attr('src', "appname://").attr('style', 'display:none;').appendTo('body'); 

为了解决这个问题,并避免不想iOS Safari的警报我已经使用了一个不同的方法处理也是一个iframe,但没有jQuery和侦听器事件。 它完美的作品。

  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 = nativeSchemaUrl; //iOS app schema url window.onload = function(){ document.body.appendChild(iframe); } setTimeout(function(){ window.location = fallbackUrl; //fallback url },300); 

我使用元刷新作为后备,因为这个工程没有JavaScript。 此代码适用于iOS和Android。

 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="refresh" content="1; url=http://www.facebook.com"> <title>Redirecting..</title> <script> function tryOpenApp() { var iframe = document.createElement("iframe"); iframe.style.border = "none"; iframe.style.width = "1px"; iframe.style.height = "1px"; iframe.src = "fb://feed/"; document.body.appendChild(iframe); } </script> </head> <body onload="tryOpenApp()"> </body> </html> 

testinghttp://static-pmoretti.herokuapp.com/deep-link/

正确的方法是使用智能应用横幅: https : //developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

如果您的应用程序未安装,横幅将让用户安装它。 如果安装了它,它将打开应用程序,并且您可以指定自定义URLparameter passing给应用程序。