Develop

[ios] iOS에서 디바이스 종류 알아오기

by hooni posted May 24, 2014
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄
다음과 같은 함수를 작성

# 플랫폼 체크
+ (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;
}
위의 함수는 클래스 함수로 정의하였으나 인스턴스 함수로 정의하여도 동작함

# 버전 체크
+ (int) machine
{
    NSString *platform = [self platform];
 
    /* iPhone */
    if ([platform isEqualToString:@"iPhone1,1"]) return 1;// @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"]) return 2;// @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return 3;// @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return 4;// @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"]) return 4;// @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"]) return 5;// @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"]) return 6;// @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"]) return 6;// @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"]) return 6;// @"iPhone 5C (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"]) return 6;// @"iPhone 5C (Global)";
    if ([platform isEqualToString:@"iPhone6,1"]) return 7;// @"iPhone 5S (GSM)";
    if ([platform isEqualToString:@"iPhone6,3"]) return 7;// @"iPhone 5S (Global)";
 
    /* iPod Touch */
    if ([platform isEqualToString:@"iPod1,1"]) return 11;// @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"]) return 12;// @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"]) return 13;// @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"]) return 14;// @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"]) return 15;// @"iPod Touch 5G";
 
    /* iPad */
    if ([platform isEqualToString:@"iPad1,1"]) return 1;// @"iPad";
    if ([platform isEqualToString:@"iPad2,1"]) return 2;// @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"]) return 2;// @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"]) return 2;// @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"]) return 2;// @"iPad 2 (Wi‑Fi Rev A)";
    if ([platform isEqualToString:@"iPad2,5"]) return 4;// @"iPad mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"]) return 4;// @"iPad mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"]) return 4;// @"iPad mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"]) return 3;// @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"]) return 3;// @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"]) return 3;// @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"]) return 4;// @"New iPad (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"]) return 4;// @"New iPad (GSM)";
    if ([platform isEqualToString:@"iPad3,6"]) return 4;// @"New iPad (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"]) return 5;// @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"iPad4,2"]) return 5;// @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,4"]) return 5;// @"iPad mini Retina (WiFi)";
    if ([platform isEqualToString:@"iPad4,4"]) return 5;// @"iPad mini Retina (Cellular)";
 
    /* Simulator */
    if ([platform isEqualToString:@"i386"]) return 3;// @"Simulator";
 
    return 0;
}


# 체크 사항
현재 위의 코드를 실행하면 Implicit declaration of function ‘sysctlbyname’ is invalid in C99 이라는 warning이 발생하지만 실행에는 문제가 없음.


[참고] http://theiphonewiki.com/wiki/Models