If Deeplink url not work send user to download page
Asked Answered
F

3

6

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

Freiburg answered 2/2, 2018 at 17:23 Comment(3)
So will this happen when user clicks on some link or on page load? Can you provide some more clarity on exactly what you are trying to achieve and what is the issue happening? Maybe post a jsfiddle or something?Attorneyatlaw
I am using the same code on $('document').ready function. purpose of this code is to open application if it is available in the system or open downloading URLFreiburg
I think you are looking at it from the wrong angle. when setting the action for your deep link, use the clearTimout function right before you redirect to "appname://". this will guarantee the time out not being set even if the browser thinks appname:// is within the same page.Deon
D
2

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.

Diplopod answered 7/10, 2020 at 9:49 Comment(0)
D
0

For desktop browser, consider using window blur listener and act accordingly Blur listener will tell you if user left the tab or browser

window.onblur=()=>{//deeplink check (maybe unsuccessfull?)
window.onfocus=()=>{//deeplink unsucesfull};

}
Darkness answered 2/2, 2018 at 17:27 Comment(0)
L
0

I would try with a timestamp expression in the timeout.

Something like this (play around with the thresholds as needed):

var clickedTm = +new date;

setTimeout(function () {
   if (+new date - clickedTm < 600) {
      window.location = "https://itunes.apple.com/appdir"; 
   }
}, 500);

window.location = "appname://";
Lipase answered 5/2, 2018 at 10:12 Comment(1)
it didn't work at all.i think timeout will not help.Freiburg

© 2022 - 2024 — McMap. All rights reserved.