Search code examples
iosmobile-safariurl-scheme

Avoid an error when trying to redirect a custom protocol scheme (appname://location) if its not registered


I'm using a "Bounce" web page that is linked from a text message to open my app. The "Bounce" web page essentiall does this:

<script type="text/javascript">
   window.location.href = "appname://location";
</script>

and works great if the appname:// protocol scheme is registered. But if it is not, an error message is displayed to the user:

Cannot Open Page
Safari cannot open the page because the address is invalid

Any ideas on how to attempt to do this redirect, but not show an error if its not working?


Solution

  • There's only a hacky javascript method to make it 'work', the invalid popup will still appear but will shortly disappear if the app is not installed, and take them to a different url

    document.location = "appname://location";
    setTimeout(function(){
        document.location = "http://google.com";
    }, 50);