iosapp-storemobile-safaricustom-url

Launching app OR app store from Safari?


I already know how to launch an app from safari, but is it possible to check if the app is installed before launching? I'm thinking to launch the app store if the app isn't currently installed on the iPhone.


Solution

  • It's not possible to check if app is installed from a web page. You could do it inside an other app by checking if your url scheme can be opened using UIApplication's -canOpenURL: method, but there is no javascript equivalent to this.

    However, you can use the following workaround:

    <script language="javascript">
        function open_appstore() {
            window.location='http://itunes.com/';
        }
    
        function try_to_open_app() {
            setTimeout('open_appstore()', 300);
        }
    </script>
    
    <a onClick="javascript:try_to_open_app();" href="yourappurl:">App name</a>
    

    This code will set a timeout on the link that will call the open_appstore function if this timeout ends. Since your link is pointed at the app's custom url, Safari will try to open that link and if it can, it will open the app and stop the timer, so AppStore link will not be opened.

    If the app link can't be opened, when timer runs out it will display an error popup saying it can't open the page (can't get rid of that), but it will immediately go to AppStore and dismiss that error.


    iOS 9 adds a really nice feature that lets your app open a http/s url: Universal Links


    In iOS 10 there is a popup saying "Open in [App Name]" when you tap the link and the app is installed. If the user does not tap on "Open" in the given timeout, this solution will use the fallback. As 300ms is too short to tap anything, this solution always fails on iOS 10.