iPhone:如果应用程序未安装,则在移动Safari中重定向到应用商店

10

我在一个优化过的移动Safari网站上有两个链接。其中一个是指向App Store以下载我的应用程序的链接,另一个是使用已注册的app://协议打开应用程序的“启动应用”按钮。问题是,如果未安装该应用程序,移动Safari会在用户单击“启动应用”按钮时产生错误。是否可以检测是否可用已注册的协议,并且如果不可用,则更改“启动应用”按钮为适当的URL,例如下载应用程序的URL,以便用户不会看到令人不快的弹出窗口?

3个回答

4
这与此问题大体相似;那里最相关的建议是使用一个单一的按钮尝试启动应用程序,同时创建一个计时器,如果应用程序未安装,则会触发计时器,因为如果已安装,则 Safari 将在计时器触发之前退出。

3
如果您在网页上添加了一个iframe,并将src设置为您的应用程序的自定义方案,iOS将自动重定向到该应用程序中的位置。如果未安装该应用程序,则不会发生任何事情。这使您可以深入链接到已安装的应用程序,或者如果未安装,则重定向到App Store。
例如,如果您已安装Twitter应用程序,并导航到包含以下标记的网页,则会立即转到应用程序。如果您没有安装Twitter应用程序,则会看到文本“未安装Twitter应用程序”。
<!DOCTYPE html>
<html>
    <head>
    <title>iOS Automatic Deep Linking</title>
    </head>
    <body>
        <iframe src="twitter://" width="0" height="0"></iframe>
        <p>The Twitter App is not installed</p>
    </body>
</html>

这意味着您可以拥有一个单独的按钮,指向具有类似于以下标记的网页:
<!DOCTYPE html>
<html>
    <head>
    <title>iOS Automatic Deep Linking</title>
    <script src='//code.jquery.com/jquery-1.11.2.min.js'></script>
    <script src='//mobileesp.googlecode.com/svn/JavaScript/mdetect.js'></script>
    <script>
      (function ($, MobileEsp) {
        // On document ready, redirect to the App on the App store.
        $(function () {
          if (typeof MobileEsp.DetectIos !== 'undefined' && MobileEsp.DetectIos()) {
            // Add an iframe to twitter://, and then an iframe for the app store
            // link. If the first fails to redirect to the Twitter app, the
            // second will redirect to the app on the App Store. We use jQuery
            // to add this after the document is fully loaded, so if the user
            // comes back to the browser, they see the content they expect.
            $('body').append('<iframe class="twitter-detect" src="twitter://" />')
              .append('<iframe class="twitter-detect" src="itms-apps://itunes.com/apps/twitter" />');
          }
        });
      })(jQuery, MobileEsp);
    </script>
    <style type="text/css">
      .twitter-detect {
        display: none;
      }
    </style>
    </head>
    <body>
    <p>Website content.</p>
    </body>
</html>

这在ios7上可以运行,但在ios8上似乎无法运行。我可以调用任何其他应用程序的URL,除了itms://或itms-app://。它在ios7上确实有效。这是否与苹果推动使用智能横幅而不是自动重定向到应用商店有关? - SuperDuperTango

1
以下是一段可用的代码片段,但并不完美。您仍然会看到Safari弹窗,但其他所有功能都按预期工作:
<script type="text/javascript">
    var timer;
    var heartbeat;
    var lastInterval;

    function clearTimers() {
        clearTimeout(timer);
        clearTimeout(heartbeat);
    }

    window.addEventListener("pageshow", function(evt){
        clearTimers();
    }, false);

    window.addEventListener("pagehide", function(evt){
        clearTimers();
    }, false);

    function getTime() {
        return (new Date()).getTime();
    }

    // For all other browsers except Safari (which do not support pageshow and pagehide properly)
    function intervalHeartbeat() {
        var now = getTime();
        var diff = now - lastInterval - 200;
        lastInterval = now;
        if(diff > 1000) { // don't trigger on small stutters less than 1000ms
            clearTimers();
        }
    }

    function launch_app_or_alt_url(el) {
        lastInterval = getTime();
        heartbeat = setInterval(intervalHeartbeat, 200);
        document.location = 'myapp://customurl';
        timer = setTimeout(function () {
            document.location = 'http://alternate.url.com';
        }, 2000);
    }

    $(".source_url").click(function(event) {
        launch_app_or_alt_url($(this));
        event.preventDefault();
    });
</script>

我在这里详细介绍了相关内容: http://aawaara.com/post/74543339755/smallest-piece-of-code-thats-going-to-change-the-world


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接