【问题标题】:How to handle deeplink redirect fails cross browser如何处理深度链接重定向失败跨浏览器
【发布时间】:2016-06-23 11:14:09
【问题描述】:

我正在尝试重定向到自定义方案以深层链接到 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 只是无法处理自定义 URL 方案并显示“地址不被理解”

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

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

最好从相应的对话框中捕获事件(如果可能)以触发重定向。 2 秒等待是任意的,可能不会总是有效。

非常感谢任何帮助。

【问题讨论】:

    标签: javascript android jquery ios internet-explorer


    【解决方案1】:

    我最终得到了这个帮助: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';
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多