当未显示正确的用户代理时,如何在iOS 13的Safari中检测设备名称? [英] How to detect device name in Safari on iOS 13 while it doesn't show the correct user agent?

查看:263
本文介绍了当未显示正确的用户代理时,如何在iOS 13的Safari中检测设备名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在苹果公司的iOS 13发布之后,我意识到在iPad iOS 13的Safari中 window.navigator.userAgent 与MacOS相同.像这样:

After Apple's iOS 13 release, I realized window.navigator.userAgent in Safari on iPad iOS 13 is same as on MacOS. Something like this:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15

如您所见,它是iPad的错误用户代理,无法检测当前设备是否为iDevice.

As you see, it's a wrong user-agent for iPad and there is no way to detect if the current device is an iDevice.

经过初步研究,我找到了解决方法:

After an initial research, I found a workaround for it:

转到设置-> Safari->请求桌面网站->所有网站.您会注意到所有网站"默认情况下启用.如果您禁用它并获取window.navigator.userAgent,则现在将显示正确的用户代理.

Go to Settings -> Safari -> Request Desktop Website -> All websites. You notice "All websites" is enabled by default. If you disable it and get window.navigator.userAgent the correct user agent is now displayed.

但是我不能要求每个用户对每个设备进行此设置更改.因此,我试图找到另一种方法,最后写了以下代码,检查它是否是Safari,macOS和触摸屏,然后该设备应该是苹果移动设备,但我想知道是有什么更好的建议/方法可以在Safari iOS 13中检测正确的设备名称?

But I cannot ask each user to do this setting change for each device. So I tried to find another way and ended up by writing the following code which checks if it's Safari, macOS, and touch-screen then the device should be an apple mobile device, but I'm wondering is there any better suggestion/way to detect the correct device name in Safari iOS 13?

detectOs = function(){
   //returns OS name, like "mac"
};

//is Safari on an apple touch-screen device
isSafariInIdevice = function(){
   if (/Safari[\/\s](\d+\.\d+)/.test(windows.navigator.userAgent)) {
      return 'ontouchstart' in window && detectOs() === "mac";      
   }
   return false;
};

推荐答案

实际上,尽管设置"中的选项更改对于用户而言可能是一个很好的解决方案,但作为开发人员,您不能依赖它.奇怪的是,要求用户不要使用暗模式,因为您的应用程序不支持它,而不是使用plist退出.

Indeed, while option change in Settings may be a good solution for the user, as a developer you can't rely on that. It is as weird as to ask the user to not to use dark mode cause your app doesn't support it instead of opt-out of it using plist.

对于我来说,立即检测iOS/iPad OS设备的最简单方法是:

As for me, the most simple way to detect iOS / iPad OS device now:

let isIOS = /iPad|iPhone|iPod/.test(navigator.platform) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)

第一个条件是老式的,并且可以在以前的版本中使用, 而第二个条件适用于iPad OS 13,后者现在将自身标识为"Mozilla/5.0(Macintosh; Intel Mac OS X 10_15)AppleWebKit/605.1.15(KHTML,如Gecko)",所有我知道的平台检测器均未检测到(目前)不能同时用作移动设备和台式机.

The first condition is old-fashioned and works with previous versions, while the second condition works for iPad OS 13 which now identifies itself as "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko)", which by all platform detectors I know is not detected (for now) neither as mobile nor desktop.

因此,由于iPad OS现在称自己为Macintosh,但实际的Mac不支持多点触摸,因此该解决方案非常适合检测iPad OS设备,它们是现有的唯一多点触摸"Macintosh"设备.

So since iPad OS now calls itself Macintosh, but real macs have no multi-touch support, this solution is ideal to detect iPad OS devices which are the only multi-touch "Macintosh" devices in existence.

PS 另外,您可能希望增强此检查功能以将IE排除在被检测为iOS设备之外

P.S. Also, you may want to augment this checkup for IE exclusion from being detected as an iOS device

let isIOS = (/iPad|iPhone|iPod/.test(navigator.platform) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) &&
!window.MSStream

这篇关于当未显示正确的用户代理时,如何在iOS 13的Safari中检测设备名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆