Ohad Milchgrub
Ohad Milchgrub

Reputation: 122

How to distinguish between iPhone 4 and iPhone 4s in javascript

I have an app which can only run from iPhone 4s and up, I need to be able to distinguish on the client side (i.e. javascript) the device, and load the app or redirect accordingly. Is there any way to detect it? thanks

Upvotes: 0

Views: 1126

Answers (1)

nzs
nzs

Reputation: 3252

I see these three options for you:

1) use a precooked open source script to check this and based on the result you do a redirect. You can use this for example: http://detectmobilebrowsers.com/

2) you implement a javascript which do a checking based on the navigator parameters. See this and a quote code from that SO answer.

function iOSversion() {
  if (/iP(hone|od|ad)/.test(navigator.platform)) {
    var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
    return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
  }
}

3.) you can use rewrite rules at Apache side to redirect to the proper page based on user-agent. See this.

For all 3 options you have a problem that it is hardly possible to distinguish between iPhone 4 and 4s. See this SO answer.

To mention only if you have to check retina capability you can use this JS too:

var isRetina = window.matchMedia("(-webkit-min-device-pixel-ratio: 2)").matches;

More info what is min-device-pixel-ratio here: link. Also info on media queries here: link

Upvotes: 1

Related Questions