简体   繁体   中英

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 ?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM