Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an app if installed through a webpage in Safari?

We have an app, lets call it: xyz_app for which I have a custom URL scheme in form of xyz_app://.

We are sending an email to the user.

when he clicks on the link, he is suppose to go to the app and in case the app is not installed, our mobile website.

so from the Link in the email, how do I make sure that if the app is not installed, that link should send the user to the mobile website instead ?

like image 734
Amogh Talpallikar Avatar asked Jul 12 '13 15:07

Amogh Talpallikar


1 Answers

The URL scheme works directly only if the app is installed on the iDevice. If not installed then it will throw an error.

1) To implement this functionality you will have to redirect the user to a webpage that will detect if app is installed, from your email link. Something like this www.yourwebsite/detectapp

2) Your detectapp page will have a javascript-

var appstoreFail = "www.your_redirect_website.com";

//Check is device is iOS
var iOS = /(iPad|iPhone|iPod)/.test(navigator.userAgent);
var appUrlScheme = "xyz_app://";

if (iOS) {
    // If the app is not installed the script will wait for 2sec and redirect to web.
    var loadedAt = +new Date;
    setTimeout(function() {
        if (+new Date - loadedAt < 2000)
            window.location = appstoreFail;
    } ,25);
    // Try launching the app using URL schemes
    window.open(appUrlScheme, "_self");
} else {
    // Launch the website
    window.location = appstoreFail;
}
like image 161
vforvendetta Avatar answered Oct 23 '22 16:10

vforvendetta