Detect phone vs tablet

后端 未结 9 828
别跟我提以往
别跟我提以往 2020-12-16 11:08

Is there a way to detect if the user is using a tablet or a phone? As an example a person surfing the web using a tablet (any android tablet with version 3+ and iPad) they

9条回答
  •  一个人的身影
    2020-12-16 11:45

    Unfortunately, as of now, the only reliable way to distinguish between mobile and tablet is via the user agent string.

    I have yet to find a user agent that includes device dimensions.

    If you try to calculate the device ppi, they almost all inevitably come out to 96ppi, whether a desktop or a phone.

    Search for iPad/iPod/iPhone in the user agent string to detect any Apple device. This will tell you if you have a tablet or phone/iPod from Apple. Fortunately, there are far fewer flavors of i-devices, so it will also be easier to distinguish amongst them.

    To identify an Android device, search for "Android" in the user agent string. To determine if it is a tablet or a phone, the phone user agent (for native android browser and chrome) will contain "Mobile Safari", whereas the tablet will only contain "Safari". This method is recommended by Google:

    As a solution for mobile sites, our Android engineers recommend to specifically detect “mobile” in the User-Agent string as well as “android.”

    A fuller description can be found here:

    http://googlewebmastercentral.blogspot.com/2011/03/mo-better-to-also-detect-mobile-user.html

    Of course this is not ideal, but looks to be the only reliable means. As a warning, from looking at a large sample list of user agent strings (near one hundred), I did see one or two older devices that did not adhere to the "Mobile" vs. "Mobile Safari" distinction properly.

提交回复
热议问题