How to detect iPhone 6 and 6 Plus using JavaScript / jQuery (or PHP)?

Is there a way to detect iPhone 6 and 6 Plus via JavaScript or PHP? I need a boolean for conditional if elsein jQuery. This is for a web application in Safari (or UIWebView). I tried to detect the screen resolution, but due to the meta-view screen or the pixel ration (I really don't know) the output value on the iPhone 5, iPhone 6 and 6 Plus is the same. Thanks in advance!

+4
source share
3 answers

With javascript, this will be a combination of two methods:

function iPhoneVersion() {
  var iHeight = window.screen.height;
  var iWidth = window.screen.width;
  if (iWidth === 320 && iHeight === 480) {
    return "4";
  }
  else if (iWidth === 375 && iHeight === 667) {
    return "6";
  }
  else if (iWidth === 414 && iHeight === 736) {
    return "6+";
  }
  else if (iWidth === 320 && iHeight === 568) {
    return "5";
  }
  else if (iHeight <= 480) {
    return "2-3";
  }
  return 'none';
}

function isIphone() {
  return !!navigator.userAgent.match(/iPhone/i);
}
Run codeHide result

So, all you have to do is check if it is an Iphone and then get the version:

if ( isIphone() && iPhoneVersion() === "6"){
  //code..
}
Run codeHide result
0

iphone 6 iphone 6 , :

    function isIphoneUp6() {
        var isIPhone = false;
        isIPhone = !!navigator.userAgent.match(/iPhone/i);

        if(isIPhone){
            var iHeight = window.screen.height;
            var iWidth = window.screen.width;
            if ((iWidth === 375 && iHeight === 667) || (iWidth === 414 && iHeight === 736)) {
                return true;
            }
        } else {        
            return false;
        }
    }


    if ( isIphoneUp6()){
      // your code
    }
0

navigator.userAgent

"Mozilla/5.0 (iPhone, iPhone 8 OS, Mac OS X) AppleWebKit/600.1.3 (KHTML, , Gecko) /8.0 Mobile/12A4345d Safari/600.1.4" iphone 6 iphone6 ​​plus

navigator.userAgent "Mozilla/5.0 (iPhone, iPhone iOS 7_0, Mac OS X, en-us) AppleWebKit/537.51.1 (KHTML, , Gecko) /7.0 Mobile/11A465 Safari/9537.53"

, iphone5 iphone6 ​​ navigator.userAgent

-2

All Articles