Search code examples
iosswiftiphone-privateapi

how to get phone's model name in swift


I've been searching a while about this and I've found out that this can't be get from UIDevice.current.model, because it returns just iPhone. there were several answers pointing to this code:

if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { 
    return simulatorModelIdentifier 
}

var sysinfo = utsname()
uname(&sysinfo) // ignore return value
let deviceModel = String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)?.trimmingCharacters(in: .controlCharacters)

return deviceModel ?? ""

by the way I'm not sure that this uses public or private api and it seems like private api for me.

Question is this code using any private api?


Solution

  • Your code does not use any private API.

    uname function returns a POSIX structure containing name and information about current kernel. The machine variable contains an hardware identifier. You'll need to convert this identifier to a more friendly name, but that's all.

    You can use man uname in your terminal for more information about the function.