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

前端 未结 1 578
北荒
北荒 2020-12-14 02:55

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.

相关标签:
1条回答
  • 2020-12-14 03:39

    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;
    }
    
    0 讨论(0)
提交回复
热议问题