Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Determine iOS device being used and other device info
//
// HKWUIDeviceHardware.h
//
#import <Foundation/Foundation.h>
@interface HKWUIDeviceHardware : NSObject
- (NSString *) platform2;
- (NSString *) platformString;
- (BOOL) isIpadOneOrTwo;
- (BOOL) isSixOrSeven;
- (BOOL) isPlus;
- (NSString *) platformStringFromPlatform;
@end
//
// HKWUIDeviceHardware.m
//
#import "HKWUIDeviceHardware.h"
#include <sys/sysctl.h>
@interface NSString (HKWPrivateAdditions)
- (BOOL)hkw_contains:(NSString *)needle;
@end
@interface HKWUIDeviceHardware ()
@property (nonatomic, strong) NSString *platform;
@property (nonatomic, strong) NSString *platformString;
@property (nonatomic, assign) BOOL isIpadOneOrTwo;
@property (nonatomic, assign) BOOL isSixOrSeven;
@property (nonatomic, assign) BOOL isPlus;
@end
@implementation HKWUIDeviceHardware
- (instancetype)init {
self = [super init];
if (!self)
return nil;
_isIpadOneOrTwo = NO;
_isSixOrSeven = NO;
_isPlus = NO;
_platform = [self platform2];
_platformString = [self platformStringFromPlatform];
#if !TARGET_IPHONE_SIMULATOR
if (([_platformString hkw_contains:@"iPhone7"] && ![_platformString hkw_contains:@"Plus"])
|| ( [_platformString hkw_contains:@"iPhone6"] && ![_platformString hkw_contains:@"Plus"])){
_isSixOrSeven = YES;
}
if ([_platformString hkw_contains:@"Plus"] ){
_isPlus = YES;
}
#endif
return self;
}
// per https://gist.github.com/Jaybles/1323251#gistcomment-866431 this runs 30% quicker thansysctlbyname
- (NSString *) platform2{
int mib[] = {CTL_HW, HW_MACHINE};
size_t len = 0;
sysctl(mib, 2, NULL, &len, NULL, 0);
char *machine = malloc(len);
sysctl(mib, 2, machine, &len, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
free(machine);
return platform;
}
- (NSString *) platformStringFromPlatform {
if ([_platform hasPrefix:@"iPad2"] || ([_platform hasPrefix:@"iPad1"])) _isIpadOneOrTwo = YES;; // this will return iPadMini too
// rearranging in order of current popularity for speed
if ([_platform isEqualToString:@"iPhone9,1"]) return @"iPhone7";
if ([_platform isEqualToString:@"iPhone9,2"]) return @"iPhone7Plus";
if ([_platform isEqualToString:@"iPhone9,3"]) return @"iPhone7";
if ([_platform isEqualToString:@"iPhone9,4"]) return @"iPhone7Plus";
if ([_platform isEqualToString:@"iPhone7,2"]) return @"iPhone6";
if ([_platform isEqualToString:@"iPhone7,1"]) return @"iPhone6Plus";
if ([_platform isEqualToString:@"iPhone8,1"]) return @"iPhone6S";
if ([_platform isEqualToString:@"iPhone8,2"]) return @"iPhone6SPlus";
if ([_platform isEqualToString:@"iPhone8,4"]) return @"iPhoneSE";
if ([_platform isEqualToString:@"iPad4,1"]) return @"iPadAir(WiFi)";
if ([_platform isEqualToString:@"iPad4,2"]) return @"iPadAir(GSM)";
if ([_platform isEqualToString:@"iPad4,3"]) return @"iPadAir(CDMA)";
if ([_platform isEqualToString:@"iPad4,4"]) return @"iPadMinRet(WiFi)";
if ([_platform isEqualToString:@"iPad4,5"]) return @"iPadMinRet(GSM)";
if ([_platform isEqualToString:@"iPad4,6"]) return @"iPadMini2";
if ([_platform isEqualToString:@"iPad4,7"]) return @"iPadMini3";
if ([_platform isEqualToString:@"iPad4,8"]) return @"iPadMini3";
if ([_platform isEqualToString:@"iPad4,9"]) return @"iPadMini3";
if ([_platform isEqualToString:@"iPad5,1"]) return @"iPadMini4";
if ([_platform isEqualToString:@"iPad5,2"]) return @"iPadMini4";
if ([_platform isEqualToString:@"iPhone5,1"]) return @"iPhone5(GSM)";
if ([_platform isEqualToString:@"iPhone5,2"]) return @"iPhone5(CDMA)";
if ([_platform isEqualToString:@"iPhone5,3"]) return @"iPhone5C(GSM)";
if ([_platform isEqualToString:@"iPhone5,4"]) return @"iPhone5C(Global)";
if ([_platform isEqualToString:@"iPhone6,2"]) return @"iPhone5S(Global)";
if ([_platform isEqualToString:@"iPhone6,1"]) return @"iPhone5S(GSM)";
if ([_platform isEqualToString:@"iPad5,4"]) return @"iPadAir2";
if ([_platform isEqualToString:@"iPad5,3"]) return @"iPadAir2(Wifi)";
if ([_platform isEqualToString:@"iPad6,7"]) return @"iPadPro12.9";
if ([_platform isEqualToString:@"iPad6,8"]) return @"iPadPro12.9";
if ([_platform isEqualToString:@"iPad6,3"]) return @"iPadPro9.7";
if ([_platform isEqualToString:@"iPad6,4"]) return @"iPadPro9.7";
if ([_platform isEqualToString:@"iPad6,11"]) return @"iPad5(Wi-Fi)";
if ([_platform isEqualToString:@"iPad6,12"]) return @"iPad5(Cellular)";
if ([_platform isEqualToString:@"iPhone3,1"]) return @"iPhone4";
if ([_platform isEqualToString:@"iPhone4,1"]) return @"iPhone4S";
if ([_platform isEqualToString:@"iPhone2,1"]) return @"iPhone3GS";
if ([_platform isEqualToString:@"iPad3,1"]) return @"iPad3(WiFi)";
if ([_platform isEqualToString:@"iPad2,1"]) return @"iPad2(WiFi)";
if ([_platform isEqualToString:@"iPad2,2"]) return @"iPad2(GSM)";
if ([_platform isEqualToString:@"iPad2,5"]) return @"iPadMini";
if ([_platform isEqualToString:@"iPad2,5"]) return @"iPadMini";
if ([_platform isEqualToString:@"iPad2,6"]) return @"iPadMini(54)";
if ([_platform isEqualToString:@"iPad2,7"]) return @"iPadMini(55)";
if ([_platform isEqualToString:@"iPad3,3"]) return @"iPad3(4GVerizon)";
if ([_platform isEqualToString:@"iPhone1,1"]) return @"iPhone1G";
if ([_platform isEqualToString:@"iPhone1,2"]) return @"iPhone3G";
if ([_platform isEqualToString:@"iPhone3,2"]) return @"VerizoniPhone4";
if ([_platform isEqualToString:@"iPhone3,3"]) return @"VerizoniPhone4";
if ([_platform isEqualToString:@"iPod1,1"]) return @"iPodTouch1G";
if ([_platform isEqualToString:@"iPod2,1"]) return @"iPodTouch2G";
if ([_platform isEqualToString:@"iPod3,1"]) return @"iPodTouch3G";
if ([_platform isEqualToString:@"iPod4,1"]) return @"iPodTouch4G";
if ([_platform isEqualToString:@"iPod5,1"]) return @"iPodTouch5G";
if ([_platform isEqualToString:@"iPod7,1"]) return @"iPodTouch6G";
if ([_platform isEqualToString:@"iPad1,1"]) return @"iPad";
if ([_platform isEqualToString:@"iPad2,3"]) return @"iPad2(CDMA)";
if ([_platform isEqualToString:@"iPad2,4"]) return @"iPad2(32nmWiFi)";
if ([_platform isEqualToString:@"iPad3,2"]) return @"iPad3(4GATT)";
if ([_platform isEqualToString:@"iPad3,4"]) return @"iPad4(58)";
if ([_platform isEqualToString:@"iPad3,5"]) return @"iPad4(59)";
if ([_platform isEqualToString:@"iPad3,6"]) return @"iPad4(60)";
// The ever mysterious iFPGA
if ([_platform isEqualToString:@"iFPGA"]) return @"iFPGA";
if ([_platform hasSuffix:@"86"] || [_platform isEqual:@"x86_64"]){
return ([[UIScreen mainScreen] bounds].size.width < 768) ? @"iPhone Simulator" : @"iPad Simulator";
}
return _platform;
}
- (NSString *) platformString{
return _platformString;
}
- (BOOL) isIpadOneOrTwo{
return _isIpadOneOrTwo;
}
- (BOOL) isSixOrSeven{
#if TARGET_IPHONE_SIMULATOR
if ([_platformString hkw_contains:@"Simulator"] ){
char *home = "jibber";
home = getenv("SIMULATOR_MODEL_IDENTIFIER");
_platform = [NSString stringWithUTF8String:home];
_platformString = [self platformStringFromPlatform];
}
if (([_platformString hkw_contains:@"iPhone7"] && ![_platformString hkw_contains:@"Plus"])
|| ( [_platformString hkw_contains:@"iPhone6"] && ![_platformString hkw_contains:@"Plus"])){
_isSixOrSeven = YES;
}
#endif
return _isSixOrSeven;
}
- (BOOL) isPlus{
#if TARGET_IPHONE_SIMULATOR
if ([_platformString hkw_contains:@"Simulator"] ){
char *home = "jibber";
home = getenv("SIMULATOR_MODEL_IDENTIFIER");
_platform = [NSString stringWithUTF8String:home];
_platformString = [self platformStringFromPlatform];
}
if ([_platformString hkw_contains:@"Plus"] ){
_isPlus = YES;;
}
#endif
return _isPlus;
}
@end
@implementation NSString (HKWPrivateAdditions)
- (BOOL)hkw_contains:(NSString *)needle{
return ([self rangeOfString:needle].location != NSNotFound);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.