美文网首页
移动端判断是否为真机打开

移动端判断是否为真机打开

作者: 前端划水工 | 来源:发表于2018-05-24 15:29 被阅读0次

本人公司移动端要对app打开和网页打开做判断
以下是我用到的代码:
新建文件夹appInfo.js 放入tools目录下

const userAgent = navigator.userAgent
let versionText = userAgent.match(/^(Android|android|iOS)\/[0-9]{2,}/g)
let version = versionText ? versionText[0].match(/[0-9]{2,}/) : 0
let type = userAgent.match(/(?=^(Android|android|iOS)\/)[a-zA-Z]+/g) || []
let appInfo = {
  isApp: /financial\//.test(userAgent),
  version: +version[0],
  type: type[0] ? type[0].toLowerCase() : void 0
}

export default appInfo

组件中导入
import appInfo from 'tools/appInfo'
在computed生命周期里定义isShow

isShow () {
        return !(appInfo.isApp && (appInfo.version >= 26)) //app打开为false
      }

如果需要判断为iPhoneX的话

isiphonex () {
        return /iphone/gi.test(navigator.userAgent) && (screen.height === 812 && screen.width === 375)
      }

相关文章

网友评论

      本文标题:移动端判断是否为真机打开

      本文链接:https://www.haomeiwen.com/subject/ykssjftx.html