JTSHardwareInfo.m 19.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
//
//  JTSHardwareInfo.m
//  JTSHardwareInfo
//
//  Created by Jared Sinclair on 10/1/14.
//  Copyright (c) 2014 Nice Boy LLC. All rights reserved.
//

#import "JTSHardwareInfo.h"
#include <sys/types.h>
#include <sys/sysctl.h>

static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone             = @"iPhone1,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3G          = @"iPhone1,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3G_China    = @"iPhone1,2*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3GS         = @"iPhone2,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3GS_China   = @"iPhone2,1*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM       = @"iPhone3,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM_2012  = @"iPhone3,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4_CDMA      = @"iPhone3,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4S          = @"iPhone4,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4S_China    = @"iPhone4,1*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5_GSM       = @"iPhone5,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5_Global    = @"iPhone5,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5C_GSM      = @"iPhone5,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5C_Global   = @"iPhone5,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5S_GSM      = @"iPhone6,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5S_Global   = @"iPhone6,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus_China = @"iPhone7,1*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus       = @"iPhone7,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6_China     = @"iPhone7,2*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6           = @"iPhone7,2";

static NSString * JTSHardwareInfo_HardwareIdentifier_iPad               = @"iPad1,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Cellular      = @"iPad1,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_WiFi        = @"iPad2,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_GSM         = @"iPad2,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_CDMA        = @"iPad2,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_MID_2012    = @"iPad2,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_WiFi     = @"iPad2,5";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_GSM      = @"iPad2,6";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Global   = @"iPad2,7";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_3_WiFi        = @"iPad3,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_3_CDMA        = @"iPad3,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_3_GSM         = @"iPad3,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_4_WiFi        = @"iPad3,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_4_GSM         = @"iPad3,5";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_4_Global      = @"iPad3,6";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Air_WiFi      = @"iPad4,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Air_Cellular  = @"iPad4,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Air_China     = @"iPad4,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_WiFi      = @"iPad4,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_Cellular  = @"iPad4,5";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_China     = @"iPad4,6";

static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_1G       = @"iPod1,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_2G       = @"iPod2,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_3G       = @"iPod3,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_4G       = @"iPod4,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_5G       = @"iPod5,1";

@implementation JTSHardwareInfo

#pragma mark - Public

+ (NSString *)hardwareIdentifier {
    static dispatch_once_t once;
    static NSString * hardware;
    dispatch_once(&once, ^ {
        size_t size = 100;
        char *hw_machine = malloc(size);
        int name[] = {CTL_HW,HW_MACHINE};
        sysctl(name, 2, hw_machine, &size, NULL, 0);
        hardware = [NSString stringWithUTF8String:hw_machine];
        free(hw_machine);
    });
    return hardware;
}

+ (JTSHardwareFamily)hardwareFamily {
    
    static dispatch_once_t once;
    static JTSHardwareFamily family;
    
    dispatch_once(&once, ^ {
        NSString *deviceName = [UIDevice currentDevice].name;
        if ([deviceName hasPrefix:@"iPhone"]) {
            family = JTSHardwareFamily_iPhone;
        }
        else if ([deviceName hasPrefix:@"iPad"]) {
            family = JTSHardwareFamily_iPad;
        }
        else if ([deviceName hasPrefix:@"iPod"]) {
            family = JTSHardwareFamily_iPodTouch;
        }
        else {
            family = JTSHardwareFamily_Unknown;
        }
    });
    
    return family;
}

+ (JTSHardwareType)hardwareType {
    static dispatch_once_t once;
    static JTSHardwareType type;
    dispatch_once(&once, ^ {
        type = [self getCurrentHardwareType];
    });
    return type;
}

+ (NSString *)hardwareDisplayName {
    
    NSString *displayName = nil;
    
    switch ([self hardwareType]) {
            
            
        case JTSHardwareType_Simulator:
            displayName = @"iOS Simulator";
            break;
            
            
        case JTSHardwareType_NotAvailable:
        case JTSHardwareType_iPhone_Unreleased:
        case JTSHardwareType_iPad_Unreleased:
        case JTSHardwareType_iPodTouch_Unreleased:
            displayName = [[UIDevice currentDevice] name];
            break;
            
        
        case JTSHardwareType_iPhone_6_China:
            displayName = @"iPhone 6 (China)";
            break;
        case JTSHardwareType_iPhone_6:
            displayName = @"iPhone 6";
            break;
        case JTSHardwareType_iPhone_6Plus_China:
            displayName = @"iPhone 6 Plus (China)";
            break;
        case JTSHardwareType_iPhone_6Plus:
            displayName = @"iPhone 6 Plus";
            break;
        case JTSHardwareType_iPhone_5S_Global:
            displayName = @"iPhone 5s (Global)";
            break;
        case JTSHardwareType_iPhone_5S_GSM:
            displayName = @"iPhone 5s (GSM)";
            break;
        case JTSHardwareType_iPhone_5C_Global:
            displayName = @"iPhone 5c (Global)";
            break;
        case JTSHardwareType_iPhone_5C_GSM:
            displayName = @"iPhone 5c (GSM)";
            break;
        case JTSHardwareType_iPhone_5_Global:
            displayName = @"iPhone 5 (Global)";
            break;
        case JTSHardwareType_iPhone_5_GSM:
            displayName = @"iPhone 5 (GSM)";
            break;
        case JTSHardwareType_iPhone_4S_China:
            displayName = @"iPhone 4S (China)";
            break;
        case JTSHardwareType_iPhone_4S:
            displayName = @"iPhone 4S";
            break;
        case JTSHardwareType_iPhone_4_GSM_2012:
            displayName = @"iPhone 4 (GSM 2012)";
            break;
        case JTSHardwareType_iPhone_4_CDMA:
            displayName = @"iPhone 4 (CDMA)";
            break;
        case JTSHardwareType_iPhone_4_GSM:
            displayName = @"iPhone 4 (GSM)";
            break;
        case JTSHardwareType_iPhone_3GS:
            displayName = @"iPhone 3GS";
            break;
        case JTSHardwareType_iPhone_3GS_China:
            displayName = @"iPhone 3GS (China/No WiFi)";
            break;
        case JTSHardwareType_iPhone_3G:
            displayName = @"iPhone 3G";
            break;
        case JTSHardwareType_iPhone_3G_China:
            displayName = @"iPhone 3G (China/No WiFi)";
            break;
        case JTSHardwareType_iPhone:
            displayName = @"iPhone";
            break;
        

        case JTSHardwareType_iPad_Mini_Retina_China:
            displayName = @"iPad Mini Retina (China)";
            break;
        case JTSHardwareType_iPad_Mini_Retina_Cellular:
            displayName = @"iPad Mini Retina (Cellular)";
            break;
        case JTSHardwareType_iPad_Mini_Retina_WiFi:
            displayName = @"iPad Mini Retina (WiFi)";
            break;
        case JTSHardwareType_iPad_Mini_Global:
            displayName = @"iPad Mini (Global)";
            break;
        case JTSHardwareType_iPad_Mini_GSM:
            displayName = @"iPad Mini (GSM)";
            break;
        case JTSHardwareType_iPad_Mini_WiFi:
            displayName = @"iPad Mini (WiFi)";
            break;
            
            
        case JTSHardwareType_iPad_Air_China:
            displayName = @"iPad Air (China)";
            break;
        case JTSHardwareType_iPad_Air_Cellular:
            displayName = @"iPad Air (Cellular)";
            break;
        case JTSHardwareType_iPad_Air_WiFi:
            displayName = @"iPad Air (WiFi)";
            break;
            
            
        case JTSHardwareType_iPad_4_Global:
            displayName = @"iPad 4 (Global)";
            break;
        case JTSHardwareType_iPad_4_GSM:
            displayName = @"iPad 4 (GSM)";
            break;
        case JTSHardwareType_iPad_4_WiFi:
            displayName = @"iPad 4 (WiFi)";
            break;
        case JTSHardwareType_iPad_3_CDMA:
            displayName = @"iPad 3 (CDMA)";
            break;
        case JTSHardwareType_iPad_3_GSM:
            displayName = @"iPad 3 (GSM)";
            break;
        case JTSHardwareType_iPad_3_WiFi:
            displayName = @"iPad 3 (WiFi)";
            break;
        case JTSHardwareType_iPad_2_Mid_2012:
            displayName = @"iPad 2 (Mid 2012)";
            break;
        case JTSHardwareType_iPad_2_CDMA:
            displayName = @"iPad 2 (CDMA)";
            break;
        case JTSHardwareType_iPad_2_GSM:
            displayName = @"iPad (GSM)";
            break;
        case JTSHardwareType_iPad_2_WiFi:
            displayName = @"iPad 2 (WiFi)";
            break;
        case JTSHardwareType_iPad_Cellular:
            displayName = @"iPad (Cellular)";
            break;
        case JTSHardwareType_iPad:
            displayName = @"iPad";
            break;
            
            
        case JTSHardwareType_iPodTouch_5Gen:
            displayName = @"iPod Touch (5th Gen)";
            break;
        case JTSHardwareType_iPodTouch_4Gen:
            displayName = @"iPod Touch (4th Gen)";
            break;
        case JTSHardwareType_iPodTouch_3Gen:
            displayName = @"iPod Touch (3rd Gen)";
            break;
        case JTSHardwareType_iPodTouch_2Gen:
            displayName = @"iPod Touch (2nd Gen)";
            break;
        case JTSHardwareType_iPodTouch_1Gen:
            displayName = @"iPod Touch (1st Gen)";
            break;
            
            
        default:
            displayName = @"Not Available";
            break;
    }
    
    return displayName;
}

+ (BOOL)airDropIsAvailable {
    
    static dispatch_once_t once;
    static BOOL isAvailable;
    
    dispatch_once(&once, ^ {
        
        JTSHardwareType hardwareType = [self hardwareType];
        
        if (hardwareType == JTSHardwareType_NotAvailable) {
            isAvailable = YES;
        }
        else if (hardwareType == JTSHardwareType_iPhone_Unreleased) {
            isAvailable = YES;
        }
        else if (hardwareType == JTSHardwareType_iPad_Unreleased) {
            isAvailable = YES;
        }
        else if (hardwareType == JTSHardwareType_iPodTouch_Unreleased) {
            isAvailable = YES;
        }
        else {
            NSString *hardwareString = [self hardwareIdentifier];
            if ([hardwareString hasPrefix:@"iPhone"]) {
                isAvailable = (hardwareType >= JTSHardwareType_iPhone_5_GSM);
            }
            else if ([hardwareString hasPrefix:@"iPad"]) {
                isAvailable = (hardwareType >= JTSHardwareType_iPad_4_WiFi);
            }
            else if ([hardwareString hasPrefix:@"iPod"]) {
                isAvailable = (hardwareType >= JTSHardwareType_iPodTouch_5Gen);
            }
        }
    });
    
    return isAvailable;
}

#pragma mark - Private

+ (JTSHardwareType)getCurrentHardwareType {
    
    NSString *hardware = [self hardwareIdentifier];
    
    if ([hardware hasPrefix:@"iPhone"]) {
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6_China])       return JTSHardwareType_iPhone_6_China;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6])             return JTSHardwareType_iPhone_6;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus_China])   return JTSHardwareType_iPhone_6Plus_China;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus])         return JTSHardwareType_iPhone_6Plus;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5S_Global])     return JTSHardwareType_iPhone_5S_Global;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5S_GSM])        return JTSHardwareType_iPhone_5S_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5C_Global])     return JTSHardwareType_iPhone_5C_Global;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5C_GSM])        return JTSHardwareType_iPhone_5C_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5_Global])      return JTSHardwareType_iPhone_5_Global;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5_GSM])         return JTSHardwareType_iPhone_5_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4S_China])      return JTSHardwareType_iPhone_4S_China;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4S])            return JTSHardwareType_iPhone_4S;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4_CDMA])        return JTSHardwareType_iPhone_4_CDMA;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM_2012])    return JTSHardwareType_iPhone_4_GSM_2012;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM])         return JTSHardwareType_iPhone_4_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3GS_China])     return JTSHardwareType_iPhone_3GS_China;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3GS])           return JTSHardwareType_iPhone_3GS;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3G_China])      return JTSHardwareType_iPhone_3G_China;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3G])            return JTSHardwareType_iPhone_3G;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone])               return JTSHardwareType_iPhone;
        return JTSHardwareType_iPhone_Unreleased;
    }
    else if ([hardware hasPrefix:@"iPad"]) {
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_China])   return JTSHardwareType_iPad_Mini_Retina_China;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_Cellular])return JTSHardwareType_iPad_Mini_Retina_Cellular;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_WiFi])    return JTSHardwareType_iPad_Mini_Retina_WiFi;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Global])         return JTSHardwareType_iPad_Mini_Global;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_GSM])            return JTSHardwareType_iPad_Mini_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_WiFi])           return JTSHardwareType_iPad_Mini_WiFi;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Air_China])           return JTSHardwareType_iPad_Air_China;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Air_Cellular])        return JTSHardwareType_iPad_Air_Cellular;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Air_WiFi])            return JTSHardwareType_iPad_Air_WiFi;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_4_Global])            return JTSHardwareType_iPad_4_Global;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_4_GSM])               return JTSHardwareType_iPad_4_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_4_WiFi])              return JTSHardwareType_iPad_4_WiFi;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_3_CDMA])              return JTSHardwareType_iPad_3_CDMA;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_3_GSM])               return JTSHardwareType_iPad_3_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_3_WiFi])              return JTSHardwareType_iPad_3_WiFi;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_MID_2012])          return JTSHardwareType_iPad_2_Mid_2012;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_CDMA])              return JTSHardwareType_iPad_2_CDMA;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_GSM])               return JTSHardwareType_iPad_2_GSM;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_WiFi])              return JTSHardwareType_iPad_2_WiFi;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Cellular])            return JTSHardwareType_iPad_Cellular;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad])                     return JTSHardwareType_iPad;
        return JTSHardwareType_iPad_Unreleased;
    }
    else if ([hardware hasPrefix:@"iPod"]) {
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_5G])         return JTSHardwareType_iPodTouch_5Gen;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_4G])         return JTSHardwareType_iPodTouch_4Gen;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_3G])         return JTSHardwareType_iPodTouch_3Gen;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_2G])         return JTSHardwareType_iPodTouch_2Gen;
        if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_1G])         return JTSHardwareType_iPodTouch_1Gen;
        return JTSHardwareType_iPodTouch_Unreleased;
    }
    
    if ([hardware isEqualToString:@"i386"]) return JTSHardwareType_Simulator;
    if ([hardware isEqualToString:@"x86_64"]) return JTSHardwareType_Simulator;
    
    return JTSHardwareType_NotAvailable;
}

@end