본문 바로가기

iOS/Swift

[iOS] 앱 기본 정보 조회

한 번 만들어두면 쓸때 편한 앱 기본 정보 조회

 	//앱 버전 조회
    func currentAppVersion() -> String {
        if let info: [String: Any] = Bundle.main.infoDictionary,
            let currentVersion: String
              = info["CFBundleShortVersionString"] as? String {
              return currentVersion
        }
        return "nil"
    }
    
    //앱 bundle 조회
    func bundleIdentifier() -> String{
        if let value = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String {
            print("bundle", value)
            return value
        }
        return ""
    }
    
    //app 이름 조회
    func appName() -> String {
        if let info: [String: Any] = Bundle.main.infoDictionary,
            let appName: String
              = info["CFBundleName"] as? String {
              return appName
        }
        return "app name"
        
    }
    
    // 디바이스 모델 조회
    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
    }