Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect from Mobile Safari to Native iOS app (like Quora)?

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?

like image 311
Callmeed Avatar asked Mar 13 '12 16:03

Callmeed


3 Answers

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>
like image 116
padi Avatar answered Nov 03 '22 00:11

padi


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>
like image 24
Loki Avatar answered Nov 02 '22 23:11

Loki


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.

like image 34
Perception Avatar answered Nov 02 '22 22:11

Perception