Skip to content

Instantly share code, notes, and snippets.

@longdog
Created October 16, 2015 06:20
Show Gist options
  • Save longdog/a5bebf2eec06ae230e9c to your computer and use it in GitHub Desktop.
Save longdog/a5bebf2eec06ae230e9c to your computer and use it in GitHub Desktop.
ios/android custom url schema support in your web page (viber example)
var $button = document.querySelector('.mobile-button'),
$input = document.querySelector('.message-input');
$button.addEventListener('click', function (e) {
e.preventDefault();
send_msg($input.value);
});
function send_msg(text) {
var app_url = "viber://forward?text=" + encodeURIComponent(text), /* custom schema url - app is launching, if it has been installed */
url = "http://viber.com", /* if device not detected */
ios_url = "https://itunes.apple.com/app/id382617920", /* open app in app store */
android_url = "market://details?id=com.viber.voip"; /* open app in play market */
if (navigator.userAgent.match(/iPhone|iPod|iPad/i)) {
url = ios_url;
} else if (navigator.userAgent.match(/android/i)) {
url = android_url;
}
var iframe = document.getElementById("__mobilebuttontempiframe") || document.createElement('iframe');
iframe.setAttribute('id', "__mobilebuttontempiframe");
iframe.style.display = "none";
iframe.style.border = "none";
document.body.appendChild(iframe);
iframe.src = app_url;
setTimeout(function () {
window.open(url, '_top');
}, 300);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment