一个嵌入的APP的H5页面怎么判断当前是安卓还是iOS

一个嵌入的APP的H5页面怎么判断当前是安卓还是iOS

阅读 5.6k
5 个回答

之前有位小伙伴总结的,用起来还不错,你根据实际情况返回类型就可以了。

isIos: function() {
    var u = navigator.userAgent;
    if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
        // return "Android";
        return false
    } else if (u.indexOf('iPhone') > -1) { //苹果手机
        // return "iPhone";
        return true
    } else if (u.indexOf('iPad') > -1) { //iPad
        // return "iPad";
        return false
    } else if (u.indexOf('Windows Phone') > -1) { //winphone手机
        // return "Windows Phone";
        return false
    } else {
        return false
    }
},

isPC: function() { //是否为PC端
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
},

userAgent

参考zeptojs的detect模块:

android = ua.match(/(Android);?[s/]+([d.]+)?/),
ipad = ua.match(/(iPad).*OSs([d_]+)/),
ipod = ua.match(/(iPod)(.*OSs([d_]+))?/),
iphone = !ipad && ua.match(/(iPhonesOS)s([d_]+)/),

ios是个计算值:

ios = ipad || ipod || iphone

判断设备一般使用userAgent,这是我之前写过的,可以参考一下

export function deviceJudgment() {
 let u = navigator.userAgent;
 const isAndroid = (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1);
 const isIos = (!!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/));
 return { isAndroid, isIos };
}

在H5页面判断当前打开该页面的设备类型,一般都是根据userAgent来判断的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题