2012-03-13 79 views
14

在我的iPhone上,我刚刚注意到,如果我使用Google搜索(在移动Safari中)并在quora.com上选择结果,结果页面会在手机上启动本机Quora应用程序。如何从移动Safari重定向到本机iOS应用程序(如Quora)?

这是如何完成的?具体而言,它是检测用户代理和使用iOS URL方案吗?它可以告诉本机应用程序是否安装和/或重定向到应用程序商店?

回答

4

你可以做触发使用custom URL scheme,通过与iOS的运行你的应用程序注册启动您的应用程序。然后在您的网站上编写代码来检测传入的用户代理,如果检测到iOS,则生成您的自定义URL而不是常规http。

+4

那么,会发生什么如果他们没有安装应用程序? – Callmeed 2012-03-13 19:18:26

+0

好问题,因为没有一个标准的处理方法。但是这里(http://stackoverflow.com/questions/1108693/is-it-possible-to-register-a-httpdomain-based-url-scheme-for-iphone-apps-like/1109200#1109200)是本网站的另一位用户发布的体面解决方法。 – Perception 2012-03-13 19:56:14

+0

解决方法是检测您的应用程序安装时是否没有错误消息? – kkurni 2014-01-29 06:09:08

15

我重新发布一个答案,我自己有关(但最初的Ruby-on-Rails的特定的)问题在这里:Rails: redirect_to 'myapp://' to call iOS app from mobile safari

您可以使用JavaScript重定向window.location

示例代码:

<html><head> 
    <script type="text/javascript"> 
     var userAgent = window.navigator.userAgent; 
     if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) { 
     window.location = "myiosapp://" 
     } 
    </script> 
    </head> 
    <body> 
    Some html page 
    </body> 
</html> 
+0

在我的iOS 7设备上不起作用。 – Jonny 2013-10-16 09:57:19

+0

如果我以全屏模式从webclip启动webapp,则无法在iOS 7设备上使用。它不得不是webclip的全屏模式设置。 – Jonny 2013-10-16 10:02:36

+0

如果没有安装应用程序,这将不起作用。 – kkurni 2014-01-29 06:09:34

5

JS代码的只是一个小的改进,如果没有安装的应用程序,它会向用户发送到iTunes商店)

<script type="text/javascript"> 

    // detect if safari mobile 
    function isMobileSafari() { 
     return navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/) 
    } 
    //Launch the element in your app if it's already installed on the phone 
    function LaunchApp(){ 
     window.open("Myapp://TheElementThatIWantToSend","_self"); 
    }; 

    if (isMobileSafari()){ 
     // To avoid the "protocol not supported" alert, fail must open itunes store to dl the app, add a link to your app on the store 
     var appstorefail = "https://itunes.apple.com/app/Myapp"; 
     var loadedAt = +new Date; 
     setTimeout(
      function(){ 
      if (+new Date - loadedAt < 2000){ 
       window.location = appstorefail; 
      } 
      } 
     ,100); 
     LaunchApp() 

    } 

</script> 
相关问题