微信小程序适配iPhone X

1、获取设备型号

App({
  // 全局数据
  globalData: {
    // 其他数据定义 ...
    
    isIPX: false, // 当前设备是否为 iPhone X
  },
  
  // 小程序启动入口
  onLaunch: function (options) {
    // 其他启动代码...
    
    // 判断设备是否为 iPhone X
    this.checkIsIPhoneX()
  },
  
  checkIsIPhoneX: function() {
    const self = this
    wx.getSystemInfo({
      success: function (res) {
        // 根据 model 进行判断
        if (res.model.search('iPhone X') != -1) {
          self.globalData.isIPX = true
        }
        // 或者根据 screenHeight 进行判断
        // if (res.screenHeight == 812) {
        //   self.globalData.isIPX = true
        // }
      }
    })
  },

NOTE:

这里有一个小坑需要注意,在微信开发者工具中的模拟器,如果选择为 iPhone X,此时获取到的 model 值为 iPhone X,导致我以为真机也是这个值,于是直接用 if (model == 'iPhone X') 来判断,但其实真机下 model 的值为这种格式: iPhone X (GSM+CDMA)<iPhone10,3>,因此我们需要用字符串检索匹配进行判断

原文地址:https://www.cnblogs.com/lml-lml/p/11418537.html