Jak uruchomić aplikacje (facebook / twitter / etc) z przeglądarki mobilnej, ale wrócić do hiperłącza, jeśli aplikacja nie jest zainstalowana

Mam nadzieję, że istnieje jakiś sposób na wykrycie, czy schemat uri:jest zarejestrowany na urządzeniu mobilnym z poziomu przeglądarki.

IE: chciałbym sprawdzić, czy aplikacje facebook, twitter, pinterest są zainstalowane i można je uruchomić z powiązanego z nimi schematu uri:.

if(fb_isInstalled) {
    // href="fb://profile/...."
} else {
    // href="http://m.facebook.com/..."
}

Zasadniczo jeśli użytkownik ma zainstalowany facebook, a następnie uruchomić aplikację, ale wrócić do mobilnej wersji strony fb, jeśli aplikacja nie jest zainstalowana.

Author: Chase Florell, 2012-12-03

1 answers

Chyba mam rozwiązanie.

 <!-- links will work as expected where javascript is disabled-->
 <a class="intent"   
    href="http://facebook.com/someProfile"   
    data-scheme="fb://profile/10000">facebook</a>

A mój javascript działa tak.
uwaga: jest tam trochę zmieszane jQuery, ale nie musisz go używać, jeśli nie chcesz.

(function () {

    // tries to execute the uri:scheme
    function goToUri(uri, href) {
        var start, end, elapsed;

        // start a timer
        start = new Date().getTime();

        // attempt to redirect to the uri:scheme
        // the lovely thing about javascript is that it's single threadded.
        // if this WORKS, it'll stutter for a split second, causing the timer to be off
        document.location = uri;
        
        // end timer
        end = new Date().getTime();

        elapsed = (end - start);

        // if there's no elapsed time, then the scheme didn't fire, and we head to the url.
        if (elapsed < 1) {
            document.location = href;
        }
    }

    $('a.intent').on('click', function (event) {
        goToUri($(this).data('scheme'), $(this).attr('href'));
        event.preventDefault();
    });
})();

Wyrzuciłem to również jako gist , z którym można się rozwidlać i zadzierać. Możesz również dołączyć gist do jsfiddle, jeśli tak zdecydujesz.


Edit

@kmallea rozwidlił istotę i radykalnie ją uprościł. https://gist.github.com/kmallea/6784568

// tries to execute the uri:scheme
function uriSchemeWithHyperlinkFallback(uri, href) {
    if(!window.open(uri)){
        window.location = href;
    }
}
// `intent` is the class we're using to wire this up. Use whatever you like.
$('a.intent').on('click', function (event) {
    uriSchemeWithHyperlinkFallback($(this).data('scheme'), $(this).attr('href'));
    // we don't want the default browser behavior kicking in and screwing everything up.
    event.preventDefault();
});
 22
Author: Chase Florell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55