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 // } } }) },
这里有一个小坑须要注意,在微信开发者工具中的模拟器,若是选择为 iPhone X,此时获取到的 model 值为 iPhone X,致使我觉得真机也是这个值,因而直接用 if (model == 'iPhone X') 来判断,但其实真机下 model 的值为这种格式: iPhone X (GSM+CDMA)<iPhone10,3>,所以咱们须要用字符串检索匹配进行判断小程序