检测设备是否为 iOS [英] Detect if device is iOS

查看:26
本文介绍了检测设备是否为 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以检测浏览器是否在 iOS 上运行,类似于使用 Modernizr 进行功能检测的方式(尽管这显然是设备检测而不是功能检测).

I'm wondering if it's possible to detect whether a browser is running on iOS, similar to how you can feature detect with Modernizr (although this is obviously device detection rather than feature detection).

通常我更喜欢功能检测,但我需要找出设备是否是 iOS,因为它们按照这个问题处理视频的方式 YouTube API 不适用于 iPad/iPhone/非 Flash 设备

Normally I would favour feature detection instead, but I need to find out whether a device is iOS because of the way they handle videos as per this question YouTube API not working with iPad / iPhone / non-Flash device

推荐答案

检测 iOS

使用 iOS 13iPad 用户代理和平台字符串均已更改区分 iPad 和MacOS 似乎是可能的,所以下面的所有答案现在都需要考虑到这一点.

Detecting iOS

With iOS 13 iPad both User agent and platform strings are changed and differentiating between iPad and MacOS seems possible, so all answers below needs to take that into account now.

这可能是涵盖 iOS 13 的最短替代方案:

This might be the shortest alternative that also covers iOS 13:

function iOS() {
  return [
    'iPad Simulator',
    'iPhone Simulator',
    'iPod Simulator',
    'iPad',
    'iPhone',
    'iPod'
  ].includes(navigator.platform)
  // iPad on iOS 13 detection
  || (navigator.userAgent.includes("Mac") && "ontouchend" in document)
}

iOS 将是 truefalse

用户代理嗅探更危险,问题经常出现.

User Agent sniffing is more dangerous and problems appear often.

在 iPad iOS 13 上,用户代理与 MacOS 13 计算机的用户代理相同,但如果您忽略 iPad,这可能仍然有效一段时间:

On iPad iOS 13, the user agent is identical with that of a MacOS 13 computer, but if you ignore iPads this might work still for a while:

var iOS = !window.MSStream && /iPad|iPhone|iPod/.test(navigator.userAgent); // fails on iPad iOS 13

!window.MSStream 不会错误地检测 IE11,请参阅 这里此处.

The !window.MSStream is to not incorrectly detect IE11, see here and here.

注意:navigator.userAgentnavigator.platform 都可以被用户或浏览器扩展伪造.

Note: Both navigator.userAgent and navigator.platform can be faked by the user or a browser extension.

存在用于更改 userAgent 或平台的浏览器扩展程序,因为网站使用过于严厉的检测并且经常禁用某些功能,即使用户的浏览器本来可以使用该功能.

Browser extensions to change userAgent or platform exist because websites use too heavy-handed detection and often disable some features even if the user's browser would otherwise be able to use that feature.

为了缓和与用户的这种冲突,建议针对每种情况专门检测您网站需要的确切功能.然后,当用户获得具有所需功能的浏览器时,该浏览器无需额外更改代码即可正常工作.

To de-escalate this conflict with users it's recommended to detect specifically for each case the exact features that your website needs. Then when the user gets a browser with the needed feature it will already work without additional code changes.

检测 iOS 版本的最常用方法是从用户代理字符串解析.但也有特征detection推断*;

The most common way of detecting the iOS version is by parsing it from the User Agent string. But there is also feature detection inference*;

我们知道 history API 是在 iOS4 中引入的 - matchMedia APIiOS5 - <iOS6 中的 code>webAudio API - iOS7 中的 WebSpeech API 等等.

We know for a fact that history API was introduced in iOS4 - matchMedia API in iOS5 - webAudio API in iOS6 - WebSpeech API in iOS7 and so on.

注意:以下代码不可靠,如果这些 HTML5 功能中的任何一个在较新的 iOS 版本中被弃用,则会中断.您已被警告!

Note: The following code is not reliable and will break if any of these HTML5 features is deprecated in a newer iOS version. You have been warned!

function iOSversion() {

  if (iOS) { // <-- Use the one here above
    if (window.indexedDB) { return 'iOS 8 and up'; }
    if (window.SpeechSynthesisUtterance) { return 'iOS 7'; }
    if (window.webkitAudioContext) { return 'iOS 6'; }
    if (window.matchMedia) { return 'iOS 5'; }
    if (window.history && 'pushState' in window.history) { return 'iOS 4'; }
    return 'iOS 3 or earlier';
  }

  return 'Not an iOS device';
}

这篇关于检测设备是否为 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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