Skip to content

Instantly share code, notes, and snippets.

@VaslD
Created March 7, 2024 17:46
Show Gist options
  • Save VaslD/c962f3f1836ac961e14067b50db07b55 to your computer and use it in GitHub Desktop.
Save VaslD/c962f3f1836ac961e14067b50db07b55 to your computer and use it in GitHub Desktop.
extension UIDevice {
func systemVersion() -> String {
let os = switch self.userInterfaceIdiom {
case .pad: "iPadOS"
case .mac: "Mac Catalyst"
case .tv: "tvOS"
case .vision: "visionOS"
default: "iOS"
}
let version = "\(os) \(self.systemVersion)"
var name = [CTL_KERN, KERN_OSVERSION]
var length = 0
guard sysctl(&name, 2, nil, &length, nil, 0) == 0, length > 0 else { return version }
var build = [CChar](repeating: 0x00, count: length)
guard sysctl(&name, 2, &build, &length, nil, 0) == 0, length > 0 else { return version }
return "\(version) (\(String(cString: build)))"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment