OpherV
OpherV

Reputation: 6937

Detecting iPhone SE device model with JS

I have a bug in my JS webapp that seems to happen only on the iPhone SE, and not on other models. Is there a way to detect the specific device model? I thought about using the screen size, but unfortunately the iPhone SE and iPhone 5 both share the same screen (reported 320 x 568) so I can't differentiate between the two.

JS detection libraries like http://hgoebl.github.io/mobile-detect.js/ do not seem to do the trick.

How can I detect the iPhone SE (regardless of iOS version) using JS?

Addendum:

User agent doesn't seem to help here, it only targets the iOS version and not the device.

iPhone SE, iOS 10:

Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A345 Safari/602.1

iPhone 5, iOS 6:

Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25

iPhone 5s, iOS 7:

Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53

Upvotes: 2

Views: 2800

Answers (1)

robocat
robocat

Reputation: 5443

You can detect the difference by checking the CPU speed with JavaScript.

Here is an example of doing that, although they do it incorrectly. Instead use a measure of the minimum execution time, because averages are ruined by the long tail caused by large delays (background tasks, garbage collection etc).

However, in my experience you should only sniff model etc if trying to avoid a bug in an obsolete device (or version). If you have a bug that expresses itself in a recent device/version it is likely it will happen in future devices/versions. When I have found display bugs in iOS on newer devices, I think it has been due to race conditions that occur due to a faster CPU (possibly due to multi-threading). Because future devices have faster CPUs any bug gets worse after the code goes into maintenance, which is not what you want.

Instead I try to find a workaround for the bug (a workaround that works on the newer device).

I do suggest you send in a bug report if it is simplifiable and repeatable. Although in my experience Apple and Microsoft are terrible at fixing reported bugs in their browsers regardless of how important, well researched, or well written the bug is (Apple just gives zero feedback and don't fix the bug. Microsoft give feedback then just close the bug for random reasons without fixing - whatever the IE/Edge team can to avoid fixing the bug). I have also been happily astounded at how good the Chromium team have been at fixing reported bugs (even complex and subjective bugs); kudos to Google.

Upvotes: 1

Related Questions