Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Deeplink url not work send user to download page

I need to implement a javascript where I can detect if my deep link is worked or not, If it works then it should remain same but if it does not work then it must start download file. For that, i use timeout function to do it. Here is sample code I used.

setTimeout(function () { window.location = "https://itunes.apple.com/appdir"; }, 25);
window.location = "appname://";

But this code works fine on android and ios but it is creating problem while it comes to the desktop browser. In desktop browser after Deeplink works properly, timeout function do not stops and it redirects to download page.

so finally I want some event which can detect if my Deeplink is worked or not so I can set cleartimeout function to prevent redirecting to downloading URL

like image 584
hardik Avatar asked Nov 17 '22 21:11

hardik


1 Answers

I have been facing similar problem, and finally I have found a nice botched job to make it work:

var timer = null;

function setListener() {
    window.document.addEventListener("visibilitychange", function(e) {
        window.clearTimeout(timer);
        timer = null;
        window.open('','_self').close();
    });
}

function redirectAndroid() {
    setTimeout(function () { window.location = "https://itunes.apple.com/appdir"; }, 25);
    window.location = "appname://";
}

function redirecIOS() {
    setListener();
    var beforeApp = new Date().valueOf();
    window.location.href = "appname://";
    var afterApp = new Date().valueOf();
    if (afterApp - beforeApp < 100) {
        setTimeout(function() {
            "https://itunes.apple.com/appdir";      
        }, 25);
    } else {
        if (timer === null) {
            timer = setTimeout(function() {
                "https://itunes.apple.com/appdir";      
            }, 200);
        }
    }

This way, after redirecting to application, if it opens it triggers the event "visibilitychange" before the timeout function, and you clear the timeout avoiding it to redirect to web, and close the browser (if you want). If application is not installed, timeAfterApp - timeBeforeApp is not < 100 so there you set the timeout.

like image 88
Oldskultxo Avatar answered Feb 12 '23 23:02

Oldskultxo