I recently had a need to query the device properties of various iPhone in the Simulator.
For those interested , here is the code (only 42 lines)
import UIKit
class ViewController: UIViewController {
override var prefersStatusBarHidden: Bool { return false }
override var preferredStatusBarStyle : UIStatusBarStyle { return .lightContent }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .all }
override var shouldAutorotate : Bool { return true }
override func viewDidLoad() { super.viewDidLoad() }
override func viewDidAppear(_ animated: Bool) {
let w : CGFloat = UIScreen.main.bounds.size.width
let h : CGFloat = UIScreen.main.bounds.size.height
let newSafeArea = view.safeAreaInsets
let device = UIDevice().name
let scale = UIScreen.main.scale
let r = CGRect(x:newSafeArea.left,y:newSafeArea.top,width:w-newSafeArea.left-newSafeArea.right,height:h-newSafeArea.top-newSafeArea.bottom)
if isLandscape {
print(" Landscape : \(device)")
} else {
print(" Portrait : \(device)");
}
print(" Logical Size : \(Int(w)) x \(Int(h))")
print("Physical Size : \(Int(w*scale)) x \(Int(h*scale))")
print(" Scale : @\(Int(scale))x")
print(" SafeArea : ",terminator: "")
print("[\(Int(r.minX)),",terminator: "")
print("\(Int(r.minY)),",terminator: "")
print("\(Int(r.width)),",terminator: "")
print("\(Int(r.height))]")
}
private var windowScene: UIWindowScene? {
return UIApplication.shared.connectedScenes.first as? UIWindowScene
}
var isLandscape: Bool {
if #available(iOS 16.0, *) {
return windowScene?.interfaceOrientation.isLandscape ?? false
}
return UIDevice.current.orientation.isLandscape
}
}
Load this into Xcode, and select a iPhone/iPad device , run the code, the data will appear in the console window (I did this so I could copy it to other documents). Switch the orientation, and run it again.
Here is what a 3rd Gen IphoneSE reports
Portrait : iPhone SE (3rd generation)
Logical Size : 375 x 667
Physical Size : 750 x 1334
Scale : @2x
SafeArea : [0,20,375,647]
Landscape : iPhone SE (3rd generation)
Logical Size : 667 x 375
Physical Size : 1334 x 750
Scale : @2x
SafeArea : [0,0,667,375]
This works just fine with the new Xcode 15
I had tried to get it to do both orientations at the same time, but any code I found that was supposed to rotate the device programmticaly didn’t seem to work right… So if anyone has an idea, let me know