判断系统类型安卓还是ios

需求:下载app时,要判断当前设备是什么系统

1.页面创建时判断系统

created(){
    //判断系统
    let u = navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //Android终端
    let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) {
        this.system = 'Android';
    } else if (isIOS) {
                this.system = 'IOS';
    } else {
                this.system = 'pc';
    }
}

 2.点击下载时执行download函数

methods: {
            download(){
                //如果是ios用户则提示,否则直接下载
                if( this.system == 'IOS'){
                    this.showAlert = true;
                    this.alertText = 'IOS用户请前往AppStore下载'
                }else{
                    try {
                        let elemIF = document.createElement("iframe");
                        elemIF.src = 'http://cangdu.org/files/elm.apk';
                        elemIF.style.display = "none";
                        document.body.appendChild(elemIF);
                    } catch (e) {
                        alert('下载失败')
                    }
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_44427784/article/details/120675399