Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if URL scheme is supported in javascript

Not seamlessly. But there is a way similar to checking if a pop-up was blocked or not.

When you try a URL scheme which is not supported, Safari will warn the user that it doesn't know what to do with it and stay on the same page.

So if you gave your app-call some time to activate, say 300 ms, and then do something else to respond to the non-existence of the scheme.

It's not the prettiest but it works:

function startIThrown(){
  document.location = 'ithrown://restart';
  setTimeout(function(){
    if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
      document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
    }
  }, 300);
}

<a href="#" onclick="startIThrown()">Restart iThrown</a>

Here is a solution that does not show the popup when you come back from the app, it assumes you've been gone longer than 400 ms:

function startiThrown() {
    document.location = appurl;
    var time = (new Date()).getTime();
    setTimeout(function(){
        var now = (new Date()).getTime();

        if((now - time)<400) {
            if(confirm('You do not seem to have iThrown installed, do you want to go download it now?')){
            document.location = 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=293049283&mt=8&uo=6';
            }
         }
    }, 300);
}

I found pagehide event to be more robust than depending on system time. For those of us who prefers a non-jQuery favor, here is the snippet.

  var appurl = 'custom://url';
  var appstore = 'https://itunes.apple.com/us/app/your-app';

  var timeout;
  function preventPopup() {
    clearTimeout(timeout);
    timeout = null;
    window.removeEventListener('pagehide', preventPopup);
  }
  function startApp() {
    window.location = appurl;
    timeout = setTimeout(function(){
      if(confirm('You do not seem to have the App installed, do you want to go download it now?')){
        document.location = appstore;
      }
    }, 1000);
    window.addEventListener('pagehide', preventPopup);
  }

Starting from iOS 6.0 Apple presented the Smart App Banners which do what most of us are looking for:

  • Send to App Store if the app isn't installed.
  • Open the App with a specific deep link, using the app-argument param.

Include the following meta tag:

<meta name="apple-itunes-app" content="app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL">

taken from here: Safari Web Content Guide


Another great (at least working in latest browser versions) workaround is to check if the browser window has focus after a short timeout, this way you can show a dialog box to the user only if the URI scheme didn't work

HTML:

<a class="uri-link" data-uri="qobuzapp://" href="#">URI</a>​

Javascript (using jQuery here):

var windowHasFocus;

$(window).focus(function() {
  windowHasFocus = true;
}).blur(function() {
  windowHasFocus = false;
});

function goToUri(uri) {
  window.location = uri;
  setTimeout(function(){
    if (windowHasFocus) {
      if (confirm('You do not seem to have Qobuz installed, do you want to go download it now?')){
        window.location = 'http://www.qobuz.com';
      }
    }
  }, 100);
}

$('a').on('click', function(){ 
  goToUri($(this).data('uri')); 
});​