2023年如何检测macOS真实版本?(macOS 11后UA参数失效)
获取macOS真实系统版本的方法(绕过UA限制)
Chromium内核浏览器(Chrome/Edge/Opera等):用Navigator.userAgentData API
这是浏览器专门为替代传统UA推出的API,能直接拿到准确的系统版本信息,需要在HTTPS或localhost环境下使用。代码示例:if ('userAgentData' in navigator) { navigator.userAgentData.getHighEntropyValues(['platformVersion']) .then(uaData => { console.log('真实macOS版本:', uaData.platformVersion); // 返回格式比如"13.5.1",对应Ventura系统 }); }Safari浏览器:结合平台判断+API特征检测
Safari目前不支持userAgentData,只能通过不同macOS版本新增的API特性来推断。比如:function getMacOSRealVersion() { if (!navigator.platform.includes('Mac')) return null; // 检测Ventura(13.x及以上) if (typeof window.showDirectoryPicker === 'function') { return '13+'; } // 检测Monterey(12.x及以上) if (typeof structuredClone === 'function') { return '12+'; } // 检测Big Sur(11.x及以上) if (Intl.DisplayNames && typeof Intl.DisplayNames === 'function') { return '11+'; } // 10.x旧系统还能通过UA判断 const oldVersionMatch = navigator.userAgent.match(/Mac OS X (\d+_\d+)/); return oldVersionMatch ? oldVersionMatch[1].replace('_', '.') : '未知版本'; } console.log(getMacOSRealVersion());这种方法需要定期更新检测逻辑,因为新系统会带来新的API特性。
通用兜底方案:WebGL渲染器信息推断
macOS的GPU驱动版本和系统版本强绑定,可以通过WebGL获取的渲染器信息间接推断。示例:function guessMacOSVersion() { const canvas = document.createElement('canvas'); const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl'); if (!gl) return null; const rendererInfo = gl.getParameter(gl.RENDERER); // 比如Apple M1的驱动版本对应特定macOS版本,需要维护映射表 if (rendererInfo.includes('Apple M1') && rendererInfo.includes('Version 16.5')) { return '13.5'; } // 更多设备和版本的映射需要自行补充 return '需更新映射表或无法识别'; }这个方法适合特定场景,需要持续维护版本映射关系。
内容的提问来源于stack exchange,提问作者Vndroid
相关产品推荐
相关产品推荐

