Skip to content

Instantly share code, notes, and snippets.

@sjwilliams
Created October 16, 2012 19:15
Show Gist options
  • Save sjwilliams/3901349 to your computer and use it in GitHub Desktop.
Save sjwilliams/3901349 to your computer and use it in GitHub Desktop.
iOS hardware/software detection. iOS versions and apple hardware type. in javascript.
/*
IOS CHECK
Based on: http://stackoverflow.com/questions/8348139/detect-ios-version-less-than-5-with-javascript
*/
(function(){
NYTD.NYTMM.iOS = false;
NYTD.NYTMM.iOSInfo = {
userAgent: null,
version: null, // major versions of iOS: 4,5,6
minor: null, // minor versions: 1, for 5.1
update: null, // optional minor version update: 2, for 5.1.2
device: null, // iPhone, iPod, iPad
phone: false, // treat iPhone and iPods both as phones
tablet: false // iPad and maybe rumored iPad mini
};
if(/(iPhone|iPod|iPad)/i.test(navigator.userAgent)) {
NYTD.NYTMM.iOS = true;
// full user agent
NYTD.NYTMM.iOSInfo.userAgent = navigator.userAgent;
// iOS version
if (/CPU like Mac OS X/i.test(navigator.userAgent)) {
NYTD.NYTMM.iOSInfo.version = 1;
} else if (/OS [2-6]_\d(_\d)? like Mac OS X/i.test(navigator.userAgent)){
// [0] = source, [1] major version, [2] minor version, [3] optional minor version update
var parts = /OS ([2-6])_(\d)(?:_(\d))? like Mac OS X/i.exec(navigator.userAgent);
NYTD.NYTMM.iOSInfo.version = parts[1];
NYTD.NYTMM.iOSInfo.minor = parts[2];
NYTD.NYTMM.iOSInfo.update = parts[3];
}
// device
var device = /(iPhone|iPod|iPad)/i.exec(navigator.userAgent);
NYTD.NYTMM.iOSInfo.device = device[0];
NYTD.NYTMM.iOSInfo.tablet = (NYTD.NYTMM.iOSInfo.device === "iPad");
NYTD.NYTMM.iOSInfo.phone = (NYTD.NYTMM.iOSInfo.tablet) ? false : true;
// add classes to body for CSS hacks
var deviceClass = (NYTD.NYTMM.iOSInfo.phone) ? "phone" : "tablet";
jQuery('body').addClass('nytmm_iOS');
jQuery('body').addClass('nytmm_iOS_'+NYTD.NYTMM.iOSInfo.version);
jQuery('body').addClass('nytmm_iOS_'+deviceClass);
}
})();
@sjwilliams
Copy link
Author

Even if we shouldn't, we sometimes want to know what flavor of iOS we're on. A quick hack in the New York Time's Multimedia Desk's namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment