In iOS12 Apple introduced a new way to check if your device is in “flat” mode, or better is on your desk.
Let’s take a look at the reference:
https://developer.apple.com/documentation/uikit/uideviceorientation/2981866-isflat
A Boolean value indicating whether the specified orientation is face up or face down.
var isFlat: Bool { get }
How it works?
First of all, you need to recognize the orientation change, that in iOS12 is a notification and must be implemented
NotificationCenter.default.addObserver( forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main, using: didRotate )
and called:
var didRotate: (Notification) -> Void = { notification in
// the new FLAT orientation
if UIDevice.current.orientation.isFlat {
print( "UIDevice.current.orientation.isFlat " )
}
// the other orientations
switch UIDevice.current.orientation {
case .landscapeLeft:
print("landscapeLeft")
case .landscapeRight:
print("landscapeRight")
case .portrait:
print("portrait")
case .portraitUpsideDown:
print("portraitUpsideDown")
case .faceUp:
print("faceUp")
case .faceDown:
print("faceDown")
default: print("other")
}
}
In this way, you are able to detect when the phone is in flat mode, face up or face down.