Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fall back to marketplace when Android custom URL scheme not handled?

Tags:

We have an app that handles a custom URL scheme (vstream://). When someone comes to a web page that has some vstream:// content, we need to redirect them to the store if they don't have our app installed.

In iOS, we do this:

setTimeout(function() {   window.location =     "itms://itunes.apple.com/us/app/kaon-v-stream/id378890806?mt=8&uo=4"; }, 25);  window.location = "vstream:view?code=...stuff..."; 

If the window.location assignment fails, the timeout jumps over the App Store before the dialog box comes up. (I found this technique here: Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps? .)

Unfortunately, this trick is not working in Android. We detect the device server side and wrote this instead of the itms: line:

"market://details?id=com.kaon.android.vstream"; 

Trouble is, whereas iOS throws an error when you go to an unhandled url scheme, Android goes to a generated page. Therefore, the timeout never gets a chance to run.

Is there some way on a web page to explicitly test for whether a custom URL scheme is handled, or can someone suggest a hack like this one that will work in Android? (Of course, I suppose I need a hack that's going to work no matter what browser they are using, which is probably a tall order...)

UPDATE: The approaches below do not work in Jelly Bean on a Nexus 7. The new Chrome browser does not go to a generated page (so the iFrame is not needed), but there does not appear to be any way to know whether the URL scheme was handled. If it was, the timeout fires anyway. If it wasn't handled the timeout fires. If I use an onload handler and an iframe, the onload handler never fires (whether the app is installed or not). I'll update if I ever figure out how to know whether the scheme was handled...

I've removed my "Solved" on my previous solution, since it doesn't work any more.

UPDATE 2: I have a good cross-platform solution now that works on iOS, Android 4.1 with Chrome, and Android pre-Chrome. See below...

Update 3: Google broke everything again with intents. Check out the VERY nice solution I've accepted by amit_saxena down there someplace /

like image 741
Joshua Smith Avatar asked Aug 29 '11 13:08

Joshua Smith


People also ask

How do I find my app scheme URL?

Go to the Play store, find the app and view its page. Copy the URL from the browser address bar. Make a note of this as the "Android Fallback URL." You'll see a parameter called "id" followed by an equal sign (=).

What is URL scheme in Android?

In Android 1.0, the Android URI scheme deep linking mechanism was created. It allows the developer to register their app for a URI (uniform resource identifier) in the operating system for a specific device once the app is installed.

What are custom URL schemes?

Custom URL schemes provide a way to reference resources inside your app. Users tapping a custom URL in an email, for example, launch your app in a specified context. Other apps can also trigger your app to launch with specific context data; for example, a photo library app might display a specified image.


1 Answers

UPDATE: Google broke this. See the new accepted answer instead.

The key, it turns out, is the document.webkitHidden property. When you set window.location to a custom URL scheme and it opens, the browser keeps running, but that property goes to false. So you can test it to determine whether the custom URL scheme was handled.

Here's a sample, which you can view live

<html>   <head>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />     <title>Starting App...</title> <script>  var URL = "kaonkaon://product.html#malvern;6"; var MARKET = "market://details?id=com.kaon.android.lepton.kaon3d"; var ITUNES = "itms://itunes.apple.com/us/app/kaon-interactive-3d-product/id525051513?mt=8&uo=4"; var QR = "http://goo.gl/gz07g"; // this should be a shortened link back to this page  function onLoad() {      if (navigator.userAgent.match(/Android/)) {          if (navigator.userAgent.match(/Chrome/)) {              // Jelly Bean with Chrome browser             setTimeout(function() {                 if (!document.webkitHidden)                     window.location = MARKET;             }, 1000);              window.location = URL;          } else {              // Older Android browser             var iframe = document.createElement("iframe");             iframe.style.border = "none";             iframe.style.width = "1px";             iframe.style.height = "1px";             var t = setTimeout(function() {                 window.location = MARKET;             }, 1000);             iframe.onload = function () { clearTimeout(t) };             iframe.src = URL;             document.body.appendChild(iframe);          }       } else if (navigator.userAgent.match(/iPhone|iPad|iPod/)) {           // IOS          setTimeout(function() {              if (!document.webkitHidden)                  window.location = ITUNES;          }, 25);           window.location = URL;       } else {           // Not mobile          var img = document.createElement("img");          img.src = "https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="+encodeURIComponent(QR);          document.body.appendChild(img);      } } </script>   </head>   <body onload="onLoad()">   </body> </html> 
like image 118
Joshua Smith Avatar answered Sep 22 '22 12:09

Joshua Smith