import UIKit

public extension UIDevice {
    
    static func isLandscape() -> Bool? {
        
        if #available(iOS 13.0, *) {
            guard let interfaceOrientation = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.windowScene?.interfaceOrientation else { return nil }
            
            return interfaceOrientation.isLandscape
        } else {
            // Fallback on earlier versions
            return UIApplication.shared.statusBarOrientation.isLandscape
        }
        
    }
    
    static func isPortrait() -> Bool? {
        if #available(iOS 13.0, *) {
            guard let interfaceOrientation = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.windowScene?.interfaceOrientation else { return nil }
            
            return interfaceOrientation.isPortrait
        } else {
            // Fallback on earlier versions
            return UIApplication.shared.statusBarOrientation.isPortrait
        }
    }
    /**
     UUID (Universally unique identifier)
     - 앱이 달라도 Organization Identifier 가 같은 앱이면 identifierForVender가 같습니다.
     - Organization Identifier가 달라지면 값이 달라집니다.
     - Team이 달라도 Organization Identifier 같으면 같습니다.
     - 기기에 설치한 Organization Identifier가 같은 앱이 없으면 초기화됩니다.
     
     ~~~
     let deviceUUID = UIDevice.deviceUUID
     ~~~
     */
    static var deviceUUID: String? {
        guard let uuid = UIDevice.current.identifierForVendor?.uuidString else {
            return nil
        }
        
        return uuid
    }
    
    /**
     임의의 128비트 값을 생성하고 고유의 값을 리턴합니다.  주의) 항상 값이 다르게 나옵니다.
     
     ~~~
     let deviceModelName = UIDevice.deviceNSUUID
     ~~~
     */
    static var deviceNSUUID: String {
        return NSUUID().uuidString
    }
    
    // 디바이스 지역코드
    static var deviceRegionCode: String? {
        guard let regionCode = Locale.current.regionCode else {
            return nil
        }
        
        return regionCode
    }
    
    /**
     실제 기기의 언어 설정 확인 함수 (zh-Hans-KR  /  ko-KR)
     
     1. zh-Hans-KR : zh-Hans (중국어(간체))  /  KR(한국)
     - 간체(zh-Hans) / 번체(zh-Hant-KR) / 번체, 홍콩(zh-Hant-HK) )
     
     2. ko-KR : ko (한국어) /  KR(한국)
     - 주의)  마지막 문자(KR)은 국가코드 이지만 나라마다 다르게 표시되기도 해서 앞 2자리 문자만 언어코드로 사용해야됨)
     
     ~~~
     let deviceLang = UIDevice.deviceLanguageCode
     ( ko / en / ja / zh )
     ~~~
     */
    static var deviceLanguageCode: String? {
        
        let preferredLanguage = Locale.preferredLanguages[0] as String
        let array = preferredLanguage.components(separatedBy: "-")
        
        guard let languageCode = array.first else {
            return Locale.current.languageCode
        }
        
        return languageCode
    }
    
    
    // Get device system name (iOS)
    static var deviceSystemName: String {
        return UIDevice.current.systemName
    }
    
    // Get device system version (16.4)
    static var deviceSystemVersion: String {
        return UIDevice.current.systemVersion
    }
    
    // Get device name (iPhone)
    static var deviceName: String {
        return UIDevice.current.name
    }
    
    // Get device model (iPhone)
    static var deviceModel: String {
        return UIDevice.current.localizedModel
    }
    
    
    
    // Get device kinds (i phone)
    static func isiPhone() -> Bool {
        return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone
    }
    
    // Get device kinds (i pad)
    static  func isiPad() -> Bool {
        return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
    }
    
    
    /**
     기기의 모델명을 반환합니다. 예) iPhone 11 Pro Max
     
     ~~~
     let deviceModelName = UIDevice.modelName
     ~~~
     */
    static var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = 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 identifier {
        case "iPod1,1":                                 return "iPod Touch 1"
        case "iPod2,1":                                 return "iPod Touch 2"
        case "iPod3,1":                                 return "iPod Touch 3"
        case "iPod4,1":                                 return "iPod Touch 4"
        case "iPod5,1":                                 return "iPod Touch 5"
        case "iPod7,1":                                 return "iPod Touch 6"
        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,2":                               return "iPhone 6"
        case "iPhone7,1":                               return "iPhone 6P"
        case "iPhone8,1":                               return "iPhone 6s"
        case "iPhone8,2":                               return "iPhone 6sP"
        case "iPhone9,1", "iPhone9,3":                  return "iPhone 7"
        case "iPhone9,2", "iPhone9,4":                  return "iPhone 7P"
        case "iPhone8,4":                               return "iPhone SE"
        case "iPhone10,1", "iPhone10,4":                return "iPhone 8"
        case "iPhone10,2", "iPhone10,5":                return "iPhone 8P"
        case "iPhone10,3", "iPhone10,6":                return "iPhone X"
        case "iPhone11,8":                              return "iPhone XR"
        case "iPhone11,2":                              return "iPhone Xs"
        case "iPhone11,6", "iPhone11,4":                return "iPhone Xs Max"
        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 generation)"
        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"
        case "iPhone14,2":                              return "iPhone 13 Pro"
        case "iPhone14,3":                              return "iPhone 13 Pro Max"
        case "iPhone14,4":                              return "iPhone 13 Mini"
        case "iPhone14,5":                              return "iPhone 13"
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
        case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad (3rd generation)"
        case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad (4th generation)"
        case "iPad6,11", "iPad6,12":                    return "iPad (5th generation)"
        case "iPad7,5", "iPad7,6":                      return "iPad (6th generation)"
        case "iPad7,11", "iPad7,12":                    return "iPad (7th generation)"
        case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
        case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
        case "iPad11,4", "iPad11,5":                    return "iPad Air (3rd generation)"
        case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad mini"
        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 "iPad11,1", "iPad11,2":                    return "iPad mini (5th generation)"
        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) (2nd generation)"
        case "iPad7,3", "iPad7,4":                      return "iPad Pro (10.5-inch)"
        case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4":return "iPad Pro (11-inch) (3rd generation)"
        case "iPad8,9", "iPad8,10":                     return "iPad Pro (11-inch) (4nd generation)"
        case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8":return "iPad Pro (12.9-inch) (3rd generation)"
        case "iPad8,11", "iPad8,12":                    return "iPad Pro (12.9-inch) (4th generation)"
        case "AppleTV5,3":                              return "Apple TV"
        case "AppleTV6,2":                              return "Apple TV 4K"
        case "AudioAccessory1,1":                       return "HomePod"
        case "i386", "x86_64":                          return "Intel Simulator"
        case "arm64":                                   return "Silicon Simulator"
        default:                                        return identifier
        }
    }
    
    static func deviceModelName() -> String {
        print("")
        print("===============================")
        print("[ViewController >> deviceModelName() :: 디바이스 모델 명칭 확인 실시]")
        print("===============================")
        print("")
        
        // [1]. 시뮬레이터 체크 수행 실시
        var modelName = ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] ?? ""
        if modelName != nil && modelName.isEmpty == false && modelName.count>0 {
            print("")
            print("===============================")
            print("[ViewController >> deviceModelName() :: 디바이스 시뮬레이터]")
            print("[deviceModelName :: \(modelName)]")
            print("===============================")
            print("")
            
            // [리턴 반환 실시]
            return "Simulrator \(modelName)"
        }
        
        // [2]. 실제 디바이스 체크 수행 실시
        let device = UIDevice.current
        let selName = "_\("deviceInfo")ForKey:"
        let selector = NSSelectorFromString(selName)
        
        if device.responds(to: selector) { // [옵셔널 체크 실시]
            modelName = String(describing: device.perform(selector, with: "marketing-name").takeRetainedValue())
        }
        
        print("")
        print("===============================")
        print("[ViewController >> deviceModelName() :: 실제 디바이스 기기]")
        print("[deviceModelName :: \(modelName)]")
        print("===============================")
        print("")
        
        // [리턴 반환 실시]
        return "\(modelName)"
    }
}
 

 

+ Recent posts