how to prevent iOS safari alert when trying to open non-installed native app?

Dima Feldman picture Dima Feldman · Oct 31, 2013 · Viewed 30.3k times · Source

I've been looking for a way to open a native iOS app from the browser. I found a decent solution here: Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps?

This solution works great when you have the app installed. but when a user doesn't have this app installed - safari fires an error message which says "Safari cannot open the page because the address is invalid."

Is there a way to prevent this behaviour and instead to prompt the user to download the app?

Answer

ElizaS picture ElizaS · Jul 31, 2014

Here is a solution that works for me:

var timeout;

function preventPopup() {
    clearTimeout(timeout);
    timeout = null;
    window.removeEventListener('pagehide', preventPopup);
}

function openApp() {    
    $('<iframe />')
    .attr('src', appurl)
    .attr('style', 'display:none;')
    .appendTo('body');

    timeout = setTimeout(function() {
            document.location = appstore;
    }, 500);
    window.addEventListener('pagehide', preventPopup);
}