The
"sysctl.proc_translated" suggestions are only going to tell you if the an Intel (x86_64) binary is running via Rosetta 2 on an Apple Silicon based Mac.
If you want to check the architecture at runtime, you can use
uname(_:) to do just that. I added an extension to
ProcessInfo to provide a bit more of a modern API for it:
Code Block swiftextension ProcessInfo { |
/// Returns a `String` representing the machine hardware name or nil if there was an error invoking `uname(_:)` or decoding the response. |
/// |
/// Return value is the equivalent to running `$ uname -m` in shell. |
var machineHardwareName: String? { |
var sysinfo = utsname() |
let result = uname(&sysinfo) |
guard result == EXIT_SUCCESS else { return nil } |
|
let data = Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)) |
|
guard let identifier = String(bytes: data, encoding: .ascii) else { return nil } |
return identifier.trimmingCharacters(in: .controlCharacters) |
} |
} |
On an Apple Silicon based mac, this will return
arm64 whereas on an Intel one it'll be
x86_64. Be aware however that if your program is running in Rosetta then it'll return
x86_64 again... You could combine it with the other solutions to check that it's also not running as translated to overcome that edge case.