Skip to content

Instantly share code, notes, and snippets.

@jdarowski
Forked from Jaybles/UIDeviceHardware.h
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdarowski/54f07ceb1d9975a97ad1 to your computer and use it in GitHub Desktop.
Save jdarowski/54f07ceb1d9975a97ad1 to your computer and use it in GitHub Desktop.
UIDeviceHardware - Determine iOS device being used

Some ways to use this class

NSString *platform = [UIDeviceHardware platform]; // to check the actual device model string
NSString *platformString = [UIDeviceHardware platformString]; // to check what this device is
UIDeviceType type = [UIDeviceHardware deviceType]; // to check the type
BOOL isThisAniPhone = [UIDeviceHardware deviceIsKindOfType:UIDeviceTypeiPhone]; // to check if this device is an iPhone
BOOL isThisNotASimulator = [UIDeviceHardware deviceIsKindOfType:~UIDeviceTypeSimulator]; // to check if the device is **not** a simulator (notice bitwise negation)
BOOL isThisAniPodOrAniPhone = [UIDeviceHardware deviceIsKindOfType:UIDeviceTypeiPhone|UIDeviceTypeiPod]; // to check if this device is an iPhone or an iPod (notice bitwise alternative)

Convenience methods

BOOL isThisAniPhone = [UIDeviceHardware deviceIsiPhone]; 
BOOL isThisAniPod = [UIDeviceHardware deviceIsiPod]; 
BOOL isThisAniPad = [UIDeviceHardware deviceIsiPad];
BOOL isThisASimulator = [UIDeviceHardware deviceIsSimulator]; 
//
// UIDeviceHardware.h
//
// Used to determine EXACT version of device software is running on.
//
// Based shamelessly upon https://gist.github.com/Jaybles/1323251
//
#import <Foundation/Foundation.h>
/**
* An enum to represent device types
*/
typedef NS_ENUM(NSUInteger, UIDeviceType){
UIDeviceTypeiPhone = 1,
UIDeviceTypeiPod = 2,
UIDeviceTypeiPad = 4,
UIDeviceTypeSimulator = 4096
};
@interface UIDeviceHardware : NSObject
/**
*
* Fetches system platform.
*
* @return Literal system platform string
*/
+ (NSString *) platform;
/**
*
* Fetches system platform and translates it to a human-readable form.
*
* @return Human-readable system platform string
*/
+ (NSString *) platformString;
/**
*
* Checks the platform strings and looks for platform keywords in it.
*
* @return Platform device type
*/
+ (UIDeviceType) deviceType;
/**
*
* Checks whether the platform is of specified type(s).
*
* @param type UIDeviceType Platform type(s) to check against.
*
* @return YES if device belongs to type, otherwise NO.
*/
+ (BOOL) deviceIsKindOfType:(UIDeviceType) type;
/**
*
* Convenience method for checking if the device is an iPhone.
*
* @return YES if device is an iPhone, otherwise NO.
*/
+ (BOOL) deviceIsiPhone;
/**
*
* Convenience method for checking if the device is an iPod.
*
* @return YES if device is an iPod, otherwise NO.
*/
+ (BOOL) deviceIsiPod;
/**
*
* Convenience method for checking if the device is an iPad.
*
* @return YES if device is an iPad, otherwise NO.
*/
+ (BOOL) deviceIsiPad;
/**
*
* Convenience method for checking if the device is a simulator.
*
* @return YES if device is a simulator, otherwise NO.
*/
+ (BOOL) deviceIsSimulator;
@end
//
// UIDeviceHardware.m
//
// Used to determine EXACT version of device software is running on.
//
// Based shamelessly upon https://gist.github.com/Jaybles/1323251
//
#import "UIDeviceHardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@implementation UIDeviceHardware
+ (NSString *) platform{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithUTF8String:machine];
free(machine);
return platform;
}
+ (NSString *) platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
else if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
else if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
else if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
else if ([platform isEqualToString:@"iPhone3,3"]) return @"Verizon iPhone 4";
else if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
else if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)";
else if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)";
else if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)";
else if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (GSM+CDMA)";
else if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)";
else if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (GSM+CDMA)";
else if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
else if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
else if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s Plus";
else if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s";
else if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
else if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
else if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
else if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
else if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
else if ([platform isEqualToString:@"iPad1,1"]) return @"iPad";
else if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
else if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
else if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
else if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (WiFi)";
else if ([platform isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)";
else if ([platform isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)";
else if ([platform isEqualToString:@"iPad2,7"]) return @"iPad Mini (GSM+CDMA)";
else if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)";
else if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM+CDMA)";
else if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)";
else if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
else if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)";
else if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (GSM+CDMA)";
else if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)";
else if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)";
else if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2G (WiFi)";
else if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2G (Cellular)";
else if ([platform isEqualToString:@"i386"]) return @"Simulator";
else if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
return platform;
}
+ (BOOL) deviceIsKindOfType:(UIDeviceType) type {
UIDeviceType thisDeviceType = [self deviceType];
return (thisDeviceType & type) > 0;
}
+(UIDeviceType)deviceType {
UIDeviceType thisDeviceType = 0;
NSString* platform = [self platform];
if ([platform hasPrefix:@"iPhone"]) {
thisDeviceType = UIDeviceTypeiPhone;
} else if ([platform hasPrefix:@"iPod"]) {
thisDeviceType = UIDeviceTypeiPod;
} else if ([platform hasPrefix:@"iPad"]) {
thisDeviceType = UIDeviceTypeiPad;
} else {
/**
* @todo
*
* In the future if more iOS devices are introduced, this will have to be extended.
*/
thisDeviceType = UIDeviceTypeSimulator;
}
return thisDeviceType;
}
+(BOOL)deviceIsiPhone {
return [self deviceIsKindOfType:UIDeviceTypeiPhone];
}
+(BOOL)deviceIsiPod {
return [self deviceIsKindOfType:UIDeviceTypeiPod];
}
+(BOOL)deviceIsiPad {
return [self deviceIsKindOfType:UIDeviceTypeiPad];
}
+(BOOL)deviceIsSimulator {
return [self deviceIsKindOfType:UIDeviceTypeSimulator];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment