Skip to content

Instantly share code, notes, and snippets.

@mikedouglas
Created August 15, 2013 19:00
Show Gist options
  • Save mikedouglas/6243706 to your computer and use it in GitHub Desktop.
Save mikedouglas/6243706 to your computer and use it in GitHub Desktop.
Detecting an launching an iOS app, or redirecting to fallback url.
<iframe id="i" style="display:none" height=0 width=0></iframe>
<script>
[...]
//// CONFIGURATION
launch_timeout = 1500;
margin_of_error = 500;
close_delay = 1000;
close_interval = 3000;
var i = document.getElementById('i');
launched_at = Date.now();
setTimeout(function() {
arrived_at = Date.now();
if (arrived_at - launched_at > launch_timeout + margin_of_error) {
set_message(MESSAGE_CLEANUP_REQUIRED);
if (history.length > 1) {
history.back();
}
else {
setTimeout( function(){ close() }, close_delay);
setInterval(function(){ close() }, close_interval);
}
} else {
set_message(MESSAGE_FALLING_BACK);
location.replace(fallback_url);
}
}, launch_timeout);
i.src = applink; // Launch!
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment