Open an app (iOS/Android) via JavaScript, with a fallback redirect to App/Play store (2016 edition)

后端 未结 2 2062
日久生厌
日久生厌 2020-12-31 19:39

Given: Website, iOS and Android applications, registered urlscheme \"myapp://\".

Goal: on the website have a link shown to iOS/Android devices with app installed. Cl

2条回答
  •  我在风中等你
    2020-12-31 20:09

    So the way it works on ios without always opening the store when the app is installed is to do something like this:

    function isIOS() {
        const iDevices = [
            'iPad Simulator',
            'iPhone Simulator',
            'iPod Simulator',
            'iPad',
            'iPhone',
            'iPod'
        ];
    
        if (navigator.platform) {
            while (iDevices.length) {
                if (navigator.platform === iDevices.pop()) {
                    return true;
                }
            }
        }
    
        return false;
    }
    
    let openedApp = false;
    
    function openAppOrStore() {
        setTimeout(function () {
            if (!openedApp) {
                window.location = "https://apps.apple.com/us/app/petleo/id1462882016";
            }
        }, 25);
        const parts = window.location.href.split('/');
        const consultationId = parts[parts.length - 2];
        const iosLink = "petleo://consultations/" + consultationId;
        try {
            window.location = iosLink;
            if (window.location.href.indexOf("petleo://") !== -1) {
                openedApp = true;
            }
        } catch (e) {   
        }
    }
    
    setTimeout(function () {
        if (window.location.href.indexOf('consultation') !== -1) {
            if (isIOS()) {
                openAppOrStore();
            }
        }
    }, 25);
    

提交回复
热议问题