15

On my iPhone, I just noticed that if I do a Google Search (in Mobile Safari) and select a result on quora.com, the result page launches the native Quora app on my phone.

How is this done? Specifically, is it a detection of the user agent and the use of an iOS URL scheme? Can it tell if the native app is installed and/or redirect to the app store?

Callmeed
  • 4,972
  • 5
  • 34
  • 49
  • Possible duplicate of [Rails: redirect\_to 'myapp://' to call iOS app from mobile safari](https://stackoverflow.com/questions/12525257/rails-redirect-to-myapp-to-call-ios-app-from-mobile-safari) – Jens Oct 07 '19 at 06:40

3 Answers3

16

I'm reposting an answer to my own related (but was originally Ruby-on-Rails-specific) question from here: Rails: redirect_to 'myapp://' to call iOS app from mobile safari

You can redirect using javascript window.location.

Sample code:

<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>
Community
  • 1
  • 1
padi
  • 787
  • 7
  • 11
  • Does not work on my iOS 7 device IF I launch the webapp from a webclip in fullscreen mode. It has to be no fullscreen mode setting for the webclip. – Jonny Oct 16 '13 at 10:02
  • Is this a proper solution? This would force redirect the user to the application every time he/she opens the URL. which it's annoying. – Reyraa Feb 06 '16 at 07:50
5

Just a small improvement of the JS code, if the app is not installed, it will send the user to itunes store ;)

<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>
Loki
  • 51
  • 1
  • 1
4

You can do trigger your application to be launched using custom URL scheme, registered by your application with the iOS runtime. Then on your website, write code to detect the incoming User-Agent and if iOS is detected generate your custom URL's instead of regular http ones.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • 4
    So, what happens if they don't have the app installed? – Callmeed Mar 13 '12 at 19:18
  • Good question, because there isn't a standard way of handling this. But [here](http://stackoverflow.com/questions/1108693/is-it-possible-to-register-a-httpdomain-based-url-scheme-for-iphone-apps-like/1109200#1109200) is a decent workaround posted by another user of this site. – Perception Mar 13 '12 at 19:56
  • Where is the workaround for detecting if your app is installed without error message ? – kkurni Jan 29 '14 at 06:09