0
votes

I'm having a problem debugging my application. In my app I need to decide if a user is on a mobile device or not base on the user agent string.

From the log info, at the beginning I saw the user agent is "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1", which should be Safari 11 on iOS 11. Then several minutes later the user agent info changed to "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1 Safari/605.1.15", which should be Safari 11.1 on macOS (High Sierra). From tealeaf I saw the user didn't change browser or device.

So my questions are:

  1. Is it possible for the user agent string changing without changing browser and device? When?

  2. For my case, why it happens?

  3. What's the most robust way to decide if a user is on mobile/tablet or desktop?

thanks.

1

1 Answers

0
votes
  1. YES. It it entirely possible to manipulate the user agent string without changing browser or device. It's possible (and quite trivial) to spoof it entirely, such as with User Agent Switcher for Chrome. Having said that, the vast majority of users won't go to the extent of spoofing it.

  2. Considering the User Agent strings thinks the browser is the same, but not the Operating System... I would assume that the user has turned on the Device Toolbar and enabled Responsive Mode in the F12 Developer Tools. This allows you to simulate the display of a website in a different aspect ratio, and does indeed manipulate the User Agent string when you select one of the available devices.

  3. It depends on what exactly you're trying to do. If you're looking for different logic based on device, I would recommend a regex. There's some great examples for different programming languages at DetectMobileBrowsers. If you're looking for a different display however, I would recommend media queries. You can cater for the width, pixel ratio and even orientation of a device. There's a great list of common queries at CSS Tricks.