Created
December 19, 2012 11:25
-
-
Save ranbena/4336044 to your computer and use it in GitHub Desktop.
A workaround to detecting if a native app is installed on the hosting mobile device.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button id="btn1">Open Facebook app</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var NativeLink = new function() { | |
var iframe, openTime, downloadLink, timeoutDelay = 300; | |
function init() { | |
// create iframe amd hide it | |
iframe = document.createElement("iframe"); | |
iframe.style.visibility = "hidden"; | |
iframe.style.position = "absolute"; | |
document.body.appendChild(iframe); | |
} | |
this.open = function(native, download) { | |
downloadLink = download; | |
openTime = new Date().getTime(); | |
iframe.src = native; | |
setTimeout(onTimeout, timeoutDelay); | |
}; | |
function onTimeout() { | |
var now = new Date().getTime(); | |
if (now - openTime < 500) { | |
window.location.href = downloadLink; | |
} | |
} | |
init(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define links | |
var native = "fb://", | |
download = "https://itunes.apple.com/en/app/facebook/id284882215"; | |
// attach listener | |
var btn1 = document.getElementById("btn1"); | |
btn1.addEventListener("click", function() { | |
NativeLink.open(legitNative, download); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment