2011-04-15 98 views
9

我在优化的移动Safari网站上有两个链接。其中一个是通过App Store下载我的应用程序的链接。另一个是启动应用程序按钮,它使用已注册的应用程序://协议来打开应用程序。问题在于,如果应用程序未安装,用户单击“启动应用程序”按钮时移动Safari会窒息。是否可以检测注册的协议是否可用,如果不可用,请使用适当的URL(如下载应用程序URL)更改Launch App按钮,以便用户不会遇到令人讨厌的弹出窗口?iPhone:如果未安装应用程序,则重定向到移动Safari浏览器上的应用商店

回答

4

这与this question大致相似;最相关的建议是只有一个按钮试图启动应用程序,同时创建一个计时器,如果未安装该应用程序,该计时器将会触发,如果该计时器在计时器启动之前就会退出。

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

3

如果用src设置为自定义方案您的网页上添加iframe对于您的应用程序,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> 
+0

这部作品ios7,但似乎并没有对iOS8上工作。我可以调用除itms之外的任何其他应用程序URL://或itms-app://。它可以在ios7上运行。这是否与苹果推动使用智能横幅而不是自动重定向到应用商店? – SuperDuperTango 2015-06-04 05:36:47