LoginSignup
2
3

More than 5 years have passed since last update.

JSでiOSのデバイス判定してみたり

Posted at

何かいい方法ないかなー・ω・`

getDevice.js
function getDevice(){
    var ua = navigator.userAgent;
    var device = 0;
    if(ua.match(/iPhone/i)) device = 1;
    if(ua.match(/iPad/i)) device = 2;
    if(ua.match(/iPod/i)) device = 3;

    switch (device ) {
        case 1:
            if( window.devicePixelRatio >= 2 ) {
                if ( screen.height >= 568 ) return "iPhone5";
                else return "iPhone4/4S";
            } else {
                return "iPhone3/3S"
            }
            break;
        case 2:
            if( window.devicePixelRatio >= 2 ) {
                return "iPad2"
            } else {
                return "iPad/mini"
            }
            break;
        case 3:
            if( window.devicePixelRatio >= 2 ) {
                if ( screen.height >= 568 ) return "iPod5";
                else return "iPod4";
            } else {
                return "iPod3"
            }
        default:
        return "NotiOS";
    }
    return "Error";
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3