While creating UI testing scripts to create screenshots, it became necessary to detect which iOS device is currently loaded in the Simulator, specifically whether its is an iPhone or an iPad. This can be done by inspecting environment variables like SIMULATOR_DEVICE_NAME
:
// Assume iPad as default
var deviceIsPhone = false
override func setUp() {
super.setup()
// The variable is only present in the simulator
if let simDeviceName = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"] {
print("Testing Device: \(simDeviceName)")
deviceIsPhone = simDeviceName.hasPrefix("iPhone")
}
}
Other helpful variables are:
"SIMULATOR_MODEL_IDENTIFIER": "iPad5,4"
"SIMULATOR_DEVICE_NAME": "iPad Air 2"
"SIMULATOR_MAINSCREEN_HEIGHT": "2048"
"SIMULATOR_MAINSCREEN_WIDTH": "1536"
"SIMULATOR_MAINSCREEN_SCALE": "2.000000"
Also, to list all variables in your simulator log, try this:
print(ProcessInfo().environment.debugDescription.split(separator: ",").joined(separator: "\n"))