JavaScript
iOS

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

More than 5 years have passed since last update.

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

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";
}