43

我一直在寻找一种从浏览器打开原生 iOS 应用程序的方法。我在这里找到了一个不错的解决方案:Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps?

安装应用程序后,此解决方案效果很好。但是当用户没有安装这个应用程序时 - Safari 会触发一条错误消息,上面写着“Safari 无法打开页面,因为地址无效。”

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

4

6 回答 6

27

这是一个适合我的解决方案:

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);
} 
于 2014-07-31T09:04:26.487 回答
6

使用 iframe。

$('<iframe />').attr('src', "appname://").attr('style', 'display:none;').appendTo('body');
于 2014-07-11T04:22:39.933 回答
2

为了解决这个问题并避免不需要的 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);
于 2015-06-03T11:32:43.740 回答
1

我使用元刷新作为后备,因为这在没有 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>

测试http://static-pmoretti.herokuapp.com/deep-link/

于 2015-08-02T21:12:42.910 回答
-1

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

如果您的应用未安装,横幅将让用户安装它。如果已安装,它将打开应用程序,并且您可以指定自定义 URL 参数以传递给应用程序。

于 2014-03-19T09:36:52.863 回答
-1

For me this does the job:

window.open(appLink, '_system')`

window.open(appstoreLink, '_system')

Works for android and ios. If the first one can't be opened the second one is called without alert or something. Otherwise it just opens the app.

于 2020-01-13T10:29:39.567 回答