How to detect iOS 6 and all minor versions by user agent?

折月煮酒 提交于 2019-11-26 16:46:13

问题


How to detect iOS 6 and all minor versions by user agent with a simple regex or something? Would be nice to distinct between iphone and ipad as well.


This question differs from What is the iOS 6 user agent string? since I wanted help in building a regex based on the information I already know (which can be found in the linked question)


回答1:


CHANGELOG

20/01/2017

  • UA strings update: 141 matching, 0 partially matching, and 797 invalid lines

  • "Mobile Safari 1.1.3 (iPhone U; CPU like Mac OS X; en)" is considered invalid even if it mentions iPhone. Actually a semi-colon is missing after the iPhone term which is suspicious.

  • Safari 8+ doesn't appear yet on UserAgentString.com

PREREQUISITES

  • The following regex is generic.
  • It will match Safari user agent strings (SUAS) running on a mobile device (see below supported devices list).
  • The regex is based on known SUAS by UserAgentString.com as of 20/01/2017.

REGEX

^(?:(?:(?:Mozilla/\d\.\d\s*\()+|Mobile\s*Safari\s*\d+(?:\.\d+)+\s*)(?:iPhone(?:\s+Simulator)?|iPad|iPod);\s*(?:U;\s*)?(?:[a-z]+(?:-[a-z]+)?;\s*)?CPU\s*(?:iPhone\s*)?(?:OS\s*\d+_\d+(?:_\d+)?\s*)?(?:like|comme)\s*Mac\s*O?S?\s*X(?:;\s*[a-z]+(?:-[a-z]+)?)?\)\s*)?(?:AppleWebKit/\d+(?:\.\d+(?:\.\d+)?|\s*\+)?\s*)?(?:\(KHTML,\s*(?:like|comme)\s*Gecko\s*\)\s*)?(?:(?:Version|CriOS)/\d+(?:\.\d+)+\s*)?(?:Mobile/\w+\s*)?(?:Safari/\d+(?:\.\d+)*.*)?$

SUPPORTED DEVICES LIST

  • iPhone
  • iPhone Simulator
  • iPad
  • iPod

EXPLANATION / CUSTOMIZATION

Lines preceded by a C can be customized.

  1. ^(?:(?:(?:Mozilla/\d\.\d\s*\()+|Mobile\s*Safari\s*\d+(?:\.\d+)+\s*)
C 2.     (?:iPhone(?:\s+Simulator)?|iPad|iPod);\s*
  3.     (?:U;\s*)?
  4.     (?:[a-z]+(?:-[a-z]+)?;\s*)?
  5.     CPU\s*
  6.          (?:iPhone\s*)?
C 7.          (?:OS\s*\d+_\d+(?:_\d+)?\s*)?
C 8.          (?:like|comme)\s*Mac\s*O?S?\s*X
  9.      (?:;\s*[a-z]+(?:-[a-z]+)?)?
  10. \)\s*)?
  11. (?:AppleWebKit/\d+(?:\.\d+(?:\.\d+)?|\s*\+)?\s*)?
C 12. (?:\(KHTML,\s*(?:like|comme)\s*Gecko\s*\)\s*)?
C 13. (?:(?:Version|CriOS)/\d+(?:\.\d+)+\s*)?
  14. (?:Mobile/\w+\s*)?
  15. (?:Safari/\d+(?:\.\d+)*.*)?$

line 1. UA strings may differ so this line is introduced for accepting UA strings as much as possible.
line 2. You can specify here the piped-separated list of accepted devices.
(...)
line 7. The version is indicated here. Change this line if you want a special version. Don't forget to update line 13 too. For instance, matching iOS 5.x.y use (?:OS\s*5_\d+_\d+\s*)?.
line 8. Some user agent strings are translated. The word like may be translated into a foreign language. The regexp now supports English and French. Adapt this line if you encounter other languages. Don't forget to update line 12 too.
(...)
line 12. See line 8.
line 13. See line 7. For instance, matching iOS 5.x.y use (?:Version/5\.\d+\.\d+)?\s*)?.



来源:https://stackoverflow.com/questions/12567260/how-to-detect-ios-6-and-all-minor-versions-by-user-agent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!