Tell iPadOS from macOS on the web

后端 未结 4 1497
南笙
南笙 2020-12-08 15:24

The user agent of Safari on iPadOS beta is at this point exactly the same as Safari on macOS. Is there any other way to tell an iPad from a Mac?

iPad running         


        
4条回答
  •  借酒劲吻你
    2020-12-08 16:13

    as @Jonny mentioned you can try and detect touch events, this is the solution i currently use

    function isTouchDevice() {
      if (typeof window === 'undefined') return false
      const prefixes = ' -webkit- -moz- -o- -ms- '.split(' ')
      function mq(query) {
        return typeof window !== 'undefined' && window.matchMedia(query).matches
      }
      if ('ontouchstart' in window || (window?.DocumentTouch && document instanceof DocumentTouch)) return true
      const query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('') // include the 'heartz' - https://git.io/vznFH
      return mq(query)
    }
    

    interesting post on detecting touch events

    and another note, Firefox on Ipad uses the same safari user-agent :/

提交回复
热议问题