Device Information & infoDictionary

YongJunCha·2021년 9월 28일
0

swift

목록 보기
9/18
post-thumbnail

Why we use this?

  • 오류 리포팅을 할 때 모바일은 기기별 대응을 해야하는 경우가 있다.
  • 따라서 오류 로그를 남길 때 기기 정보를 남기는 편이 좋다.
    ///Identifier 찾기
    public func getDeviceIdentifier() -> 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)))
        }
        
        return identifier
    }

    /**
     디바이스 모델 (iPhone, iPad) 이름 전달 (iPhone6, iPhone7 Plus...)
     */
    public func deviceModelName() -> String {
        
        let model = UIDevice.current.model
        
        switch model {
        case "iPhone":
            return iPhoneModel()
        case "iPad":
            return iPadModel()
        case "iPad mini" :
            return iPadMiniModel()
        default:
            return "Unknown Model : \(model)"
        }
        
    }

    /**
     iPhone 모델 이름 (iPhone6, iPhone7 Plus...)
     */
    public func iPhoneModel() -> String {
        
        let identifier = getDeviceIdentifier()
        
        switch identifier {
        case "iPod5,1":                                 return "iPod touch (5th generation)"
          case "iPod7,1":                                 return "iPod touch (6th generation)"
          case "iPod9,1":                                 return "iPod touch (7th generation)"
          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 6 Plus"
          case "iPhone8,1":                               return "iPhone 6s"
          case "iPhone8,2":                               return "iPhone 6s Plus"
          case "iPhone9,1", "iPhone9,3":                  return "iPhone 7"
          case "iPhone9,2", "iPhone9,4":                  return "iPhone 7 Plus"
          case "iPhone8,4":                               return "iPhone SE"
          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 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 "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)"
          case "iPad8,9":                                 return "iPad Pro (11-inch) (2nd generation)"
          case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8":return "iPad Pro (12.9-inch) (3rd generation)"
          case "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"
        default:
            return "Unknown iPhone : \(identifier)"
        }
    }

    /**
     iPad 모델 이름
    */
    public func iPadModel() -> String {
        
        let identifier = getDeviceIdentifier()
        
        switch identifier {
        case "iPad1,1":
            return "iPad"
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4" :
            return "iPad2"
        case "iPad3,1", "iPad3,2", "iPad3,3" :
            return "iPad 3rd Generation"
        case "iPad3,4", "iPad3,5", "iPad3,6" :
            return "iPad 4rd Generation"
        case "iPad4,1", "iPad4,2", "iPad4,3" :
            return "iPad Air"
        case "iPad5,3", "iPad5,4" :
            return "iPad Air2"
        case "iPad6,7", "iPad6,8" :
            return "iPad Pro 12.9"
        case "iPad6,3", "iPad6,4" :
            return "iPad Pro 9.7"
        case "iPad6,11", "iPad6,12" :
            return "iPad 5th Generation"
        case "iPad7,1", "iPad7,2" :
            return "iPad Pro 12.9 2nd Generation"
        case "iPad7,3", "iPad7,4" :
            return "iPad Pro 10.5"
        case "iPad7,5", "iPad7,6" :
            return "iPad 6th Generation"
        default:
            return "Unknown iPad : \(identifier)"
        }
    }

    /**
     iPad mini 모델 이름
     */
    public  func iPadMiniModel() -> String {
        
        let identifier = getDeviceIdentifier()
        
        switch identifier {
        case "iPad2,5", "iPad2,6", "iPad2,7" :
            return "iPad mini"
        case "iPad4,4", "iPad4,5", "iPad4,6" :
            return "iPad mini2"
        case "iPad4,7", "iPad4,8", "iPad4,9" :
            return "iPad mini3"
        case "iPad5,1", "iPad5,2" :
            return "iPad mini4"
        default:
            return "Unknown iPad mini : \(identifier)"
        }
    }
  • identifier는 새로운 아이폰이 나올 때 마다 수동으로 넣어줘야한다.
      // 사용자 버전, 기기, DID, OS 정보
        var addDefaultInfo : String?  {
            guard let infomation = Bundle.main.infoDictionary,
                let appName = infomation["CFBundleDisplayName"] as? String,
                let appVersion = infomation["CFBundleShortVersionString"] as? String,
                let appBuildVersion = infomation["CFBundleVersion"] as? String,
                let bundleIdentifier = infomation["CFBundleIdentifier"] as? String else {return nil}

            let systemVersion = UIDevice.current.systemVersion
            let versionAndBuild = "\n\n \(appName)\n VERSION_NAME : \(appVersion)\n VERSION_CODE : \(appBuildVersion)\n MODEL : \(deviceModelName())\n OS : \(systemVersion)\n DID : \(DID)\n QUESTION_TYPE : \(questionType)"
            return versionAndBuild
            }
  • 위와 같이 얻을 수 있다.

Conclusion

  • 개발 하면서 한번은 반드시 쓰게 될 것이다.
  • extension이나 final class로 따로 분리시켜도 좋을 것 같다.

0개의 댓글