iOS/Swift + Objective-c

[Swift / Objective-c] 디바이스 모델명, OS 버전 조회 (Device Model, OS Version, iPhone/iPad/iPod)

안경 쓴 귀니 2022. 2. 23. 20:21
반응형

Swift와 Objective-c에서 디바이스 모델명, OS 버전을 조회하는 방법

(iPhone, iPad, iPod)

 

iOS에서 디바이스 모델 조회 시, 아래와 같이 조회가 됩니다.  (디바이스 모델)

  • iPhone11,8
  • iPhone12,5

 

위와 같은 형식을 원하는 경우도 있지만 아래처럼 원하는 경우도 있죠. (디바이스 모델명)

  • iPhone XR
  • iPhone 11 Pro Max

 

위 두 가지를 조회하는 방법 모두 있습니다.

 

방법

 

  • Swift
// 디바이스 OS 버전 조회
func getOsVersion() -> String {
    return UIDevice.current.systemVersion
}

// 디바이스 모델 조회
func getModel() -> String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let model = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }
    return model
}

// 디바이스 모델명 조회
func getModelName() -> String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let model = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }
    
    switch model {
        // Simulator
        case "i386", "x86_64":                          return "Simulator"
        // iPod
        case "iPod1,1":                                 return "iPod Touch"
        case "iPod2,1", "iPod3,1", "iPod4,1":           return "iPod Touch"
        case "iPod5,1", "iPod7,1":                      return "iPod Touch"
        // iPad
        case "iPad1,1":                                 return "iPad"
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
        case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
        case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
        case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
        case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
        case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
        case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
        case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
        case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
        case "iPad6,11", "iPad6,12":                    return "iPad 5"
        case "iPad6,3", "iPad6,4":                      return "iPad Pro 9.7 Inch"
        case "iPad6,7", "iPad6,8":                      return "iPad Pro 12.9 Inch"
        case "iPad7,1", "iPad7,2":                      return "iPad Pro 12.9 Inch 2. Generation"
        case "iPad7,3", "iPad7,4":                      return "iPad Pro 10.5 Inch"
        // iPhone
        case "iPhone1,1", "iPhone1,2", "iPhone2,1":     return "iPhone"
        case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
        case "iPhone4,1":                               return "iPhone 4s"
        case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
        case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
        case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
        case "iPhone7,1":                               return "iPhone 6 Plus"
        case "iPhone7,2":                               return "iPhone 6"
        case "iPhone8,1":                               return "iPhone 6s"
        case "iPhone8,2":                               return "iPhone 6s Plus"
        case "iPhone8,4":                               return "iPhone SE"
        case "iPhone9,1", "iPhone9,3":                  return "iPhone 7"
        case "iPhone9,2", "iPhone9,4":                  return "iPhone 7 Plus"
        case "iPhone10,1", "iPhone10,4":                return "iPhone 8"
        case "iPhone10,2", "iPhone10,5":                return "iPhone 8 Plus"
        case "iPhone10,3", "iPhone10,6":                return "iPhone X"
        case "iPhone11,2":                              return "iPhone XS"
        case "iPhone11,4", "iPhone11,6":                return "iPhone XS Max"
        case "iPhone11,8":                              return "iPhone XR"
        case "iPhone12,1":                              return "iPhone 11"
        case "iPhone12,3":                              return "iPhone 11 Pro"
        case "iPhone12,5":                              return "iPhone 11 Pro Max"
        case "iPhone12,8":                              return "iPhone SE 2nd Gen"
        case "iPhone13,1":                              return "iPhone 12 Mini"
        case "iPhone13,2":                              return "iPhone 12"
        case "iPhone13,3":                              return "iPhone 12 Pro"
        case "iPhone13,4":                              return "iPhone 12 Pro Max"
        default:                                        return model
    }
}

 

 

  • Objective-C
#import <sys/utsname.h>

// 디바이스 OS 버전 조회
-(NSString*) getOsVersion {
    return [[UIDevice currentDevice] systemVersion];
}

// 디바이스 모델 조회
-(NSString*) getModel {
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

// 디바이스 모델명 조회
-(NSString *)getModelName {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    static NSDictionary* model = nil;
    
    if (!model) {
        model = @{
            // Simulator
            @"i386"      : @"Simulator",
            @"x86_64"    : @"Simulator",
            // iPod
            @"iPod1,1"   : @"iPod Touch",           // (Original)
            @"iPod2,1"   : @"iPod Touch",           // (Second Generation)
            @"iPod3,1"   : @"iPod Touch",           // (Third Generation)
            @"iPod4,1"   : @"iPod Touch",           // (Fourth Generation)
            @"iPod5,1"   : @"iPod Touch",           // (Fourth Generation)
            @"iPod7,1"   : @"iPod Touch",           // (6th Generation)
            // iPad
            @"iPad1,1"   : @"iPad",                 // (Original)
            @"iPad2,1"   : @"iPad 2",               //
            @"iPad2,2"   : @"iPad 2",               //
            @"iPad2,3"   : @"iPad 2",               //
            @"iPad2,4"   : @"iPad 2",               //
            @"iPad2,5"   : @"iPad Mini",            // (Original)
            @"iPad2,6"   : @"iPad Mini",            // (Original)
            @"iPad2,7"   : @"iPad Mini",            // (Original)
            @"iPad3,1"   : @"iPad 3",               // (3rd Generation)
            @"iPad3,2"   : @"iPad 3",               // (3rd Generation)
            @"iPad3,3"   : @"iPad 3",               // (3rd Generation)
            @"iPad3,4"   : @"iPad 4",               // (4th Generation)
            @"iPad3,5"   : @"iPad 4",               // (4th Generation)
            @"iPad3,6"   : @"iPad 4",               // (4th Generation)
            @"iPad4,1"   : @"iPad Air",             // 5th Generation iPad (iPad Air) - Wifi
            @"iPad4,2"   : @"iPad Air",             // 5th Generation iPad (iPad Air) - Cellular
            @"iPad4,3"   : @"iPad Air",             //
            @"iPad4,4"   : @"iPad Mini 2",          // (2nd Generation iPad Mini - Wifi)
            @"iPad4,5"   : @"iPad Mini 2",          // (2nd Generation iPad Mini - Cellular)
            @"iPad4,6"   : @"iPad Mini 2",          //
            @"iPad4,7"   : @"iPad Mini 3",          //
            @"iPad4,8"   : @"iPad Mini 3",          //
            @"iPad4,9"   : @"iPad Mini 3",          //
            @"iPad5,1"   : @"iPad Mini 4",          //
            @"iPad5,2"   : @"iPad Mini 4",          //
            @"iPad5,3"   : @"iPad Air 2",           // (3rd Generation iPad Mini - Wifi (model A1599))
            @"iPad5,4"   : @"iPad Air 2",           // (3rd Generation iPad Mini - Wifi (model A1599))
            @"iPad6,3"   : @"iPad Pro 9.7 Inch",    // iPad Pro 9.7 inches - (model A1673)
            @"iPad6,4"   : @"iPad Pro 9.7 Inch",    // iPad Pro 9.7 inches - (models A1674 and A1675)
            @"iPad6,7"   : @"iPad Pro 12.9 Inch",   // iPad Pro 12.9 inches - (model A1584)
            @"iPad6,8"   : @"iPad Pro 12.9 Inch",   // iPad Pro 12.9 inches - (model A1652)
            @"iPad7,1"   : @"iPad Pro 12.9 Inch 2. Generation",
            @"iPad7,2"   : @"iPad Pro 12.9 Inch 2. Generation",
            @"iPad7,3"   : @"iPad Pro 10.5 Inch",
            @"iPad7,4"   : @"iPad Pro 10.5 Inch",
            // iPhone
            @"iPhone1,1" : @"iPhone",            // (Original)
            @"iPhone1,2" : @"iPhone",            // (3G)
            @"iPhone2,1" : @"iPhone",            // (3GS)
            @"iPhone3,1" : @"iPhone 4",          // (GSM)
            @"iPhone3,2" : @"iPhone 4",          //
            @"iPhone3,3" : @"iPhone 4",          // (CDMA/Verizon/Sprint)
            @"iPhone4,1" : @"iPhone 4s",         //
            @"iPhone5,1" : @"iPhone 5",          // (model A1428, AT&T/Canada)
            @"iPhone5,2" : @"iPhone 5",          // (model A1429, everything else)
            @"iPhone5,3" : @"iPhone 5c",         // (model A1456, A1532 | GSM)
            @"iPhone5,4" : @"iPhone 5c",         // (model A1507, A1516, A1526 (China), A1529 | Global)
            @"iPhone6,1" : @"iPhone 5s",         // (model A1433, A1533 | GSM)
            @"iPhone6,2" : @"iPhone 5s",         // (model A1457, A1518, A1528 (China), A1530 | Global)
            @"iPhone7,1" : @"iPhone 6 Plus",     //
            @"iPhone7,2" : @"iPhone 6",          //
            @"iPhone8,1" : @"iPhone 6s",         //
            @"iPhone8,2" : @"iPhone 6s Plus",    //
            @"iPhone8,4" : @"iPhone SE",         //
            @"iPhone9,1" : @"iPhone 7",          //
            @"iPhone9,3" : @"iPhone 7",          //
            @"iPhone9,2" : @"iPhone 7 Plus",     //
            @"iPhone9,4" : @"iPhone 7 Plus",     //
            @"iPhone10,1": @"iPhone 8",          // CDMA
            @"iPhone10,4": @"iPhone 8",          // GSM
            @"iPhone10,2": @"iPhone 8 Plus",     // CDMA
            @"iPhone10,5": @"iPhone 8 Plus",     // GSM
            @"iPhone10,3": @"iPhone X",          // iPhone X A1865,A1902
            @"iPhone10,6": @"iPhone X",          // iPhone X A1901
            @"iPhone11,2": @"iPhone XS",         // iPhone XS A2097,A2098
            @"iPhone11,4": @"iPhone XS Max",     // iPhone XS Max A1921,A2103
            @"iPhone11,6": @"iPhone XS Max",     // iPhone XS Max A2104
            @"iPhone11,8": @"iPhone XR",         // iPhone XR ModelNo Unknown
            @"iPhone12,1": @"iPhone 11",
            @"iPhone12,3": @"iPhone 11 Pro",
            @"iPhone12,5": @"iPhone 11 Pro Max",
            @"iPhone12,8": @"iPhone SE 2nd Gen",
            @"iPhone13,1": @"Phone 12 Mini",
            @"iPhone13,2": @"iPhone 12",
            @"iPhone13,3": @"iPhone 12 Pro",
            @"iPhone13,4": @"iPhone 12 Pro Max"
        };
    }
    
    NSString* deviceName = [model objectForKey:code];
    if (!deviceName) {
        if ([code rangeOfString:@"iPod"].location != NSNotFound) {
            deviceName = @"iPod Touch";
        }
        else if([code rangeOfString:@"iPad"].location != NSNotFound) {
            deviceName = @"iPad";
        }
        else if([code rangeOfString:@"iPhone"].location != NSNotFound){
            deviceName = @"iPhone";
        }
        else {
            deviceName = @"Unknown";
        }
    }
    
    return deviceName;
}

 

 

리턴

 

디바이스 OS 버전 조회 결과

  • 15.2.1
  • 14.6

 

디바이스 모델 조회 결과

  • iPhone11,8
  • iPhone12,5

 

디바이스 모델명 조회 결과

  • iPhone XR
  • iPhone 11 Pro Max

 

반응형