跨浏览器如何处理deeplink重定向失败

时间:2016-06-23 11:14:09

标签: javascript android jquery ios internet-explorer

我正在尝试重定向到自定义方案以深度链接到iOS和Android应用程序。行为是不同的跨浏览器,所以我试图找到一致的解决方案。下面是我在不同浏览器中的jQuery和行为。

$(document).ready(function (){

    console.log(window.location.href);

    window.location.href = window.location.href.replace('http','custom').replace('https','custom');

    //I am waiting 2 seconds for this to succeed before trying to launch 
      device app store or www site if on an unsupported device/web browser

    window.setTimeout(function(){

        console.log(navigator.userAgent.toLowerCase());

        if(navigator.userAgent.toLowerCase().indexOf("android") > -1){

            console.log('Android');
            window.location.href = 'https://play.google.com/store/apps/details?id=';

        }else if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){

            console.log('iOS');
            window.location.href = 'https://itunes.apple.com/us/app/';

        }else{

            console.log('Other');
            window.location.href = 'https://www.website.com';

        }
    },2000);//wait for 2000 milliseconds before trying to redirect to stores. 
           //this is crap it would be better to wait for an error or respond
             to the Chrome External Protocol Request dialog

    });

Chrome启动“外部协议请求”对话框。如果在2秒内没有响应,或者用户点击了“什么都不做”。重定向工作

Firefox无法处理自定义网址方案并显示“地址未被理解'

IE启动App Store对话框并成功重定向(!)

Safari启动取消/选择应用程序/搜索应用商店对话框,但不重定向。任何互动都会导致Safari无法打开指定的地址'。

从相应的对话框中捕获事件(如果可能)以触发重定向会好得多。 2秒等待是任意的,可能不会一直工作。

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

我最后到达了这个帮助后:How to check if a custom protocol supported

这会检查深层链接代码是否存在,如果不是只是重定向。如果是,它会尝试更改window.href值,如果错误只是正常重定向。

$(document).ready(function (){

        console.log(window.location.href);

        var code = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);

        console.log(code);

        if(code != undefined && code !=""){

            console.log('Code is '+code);

            try{

                window.location.href = window.location.href.replace('http','custom').replace('https','custom');

            }catch(e){

                console.log('In error catch');

                redirectStores();

            }

        }else{
            console.log('No code here..');

            redirectStores();
        }

    });

    var redirectStores = function(){

        console.log(navigator.userAgent.toLowerCase());

        if(navigator.userAgent.toLowerCase().indexOf("android") > -1){

            console.log('Android');
            window.location.href = 'https://play.google.com/store/apps/details?id=';//add app id

        }else if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){

            console.log('iOS');
            window.location.href = 'https://itunes.apple.com/us/app/';//add app id

        }else{

            console.log('Other');
            window.location.href = 'https://www.example.com';

        }

    }
相关问题