Getting iOS device orientation right after the app launch in Swift

Atikur Rahman
Evangelist Apps Blog
2 min readMay 20, 2022
Photo credits Unknown

In iOS, we can get the physical orientation of the device using the UIDevice class as below:

UIDevice.current.orientation

So to detect if the current device orientation is landscape, you might add a computed property as an extension to UIApplication class as below:

Then you use UIApplication.shared.isLandscape to conveniently check for landscape orientation.

This works perfectly fine with device rotation, however you might get surprised sometimes when it does not work as expected. If you launch your app on landscape orientation, this UIApplication.shared.isLandscape might still give you false value (before rotating your device to portrait and rotating it back to landscape). That’s because the UIDevice.current.orientation value is .unknown right after launching the app.

In that case, to get the correct orientation value, you need to check UIWindowScene’s interfaceOrientation value. If that value is either .landscapeLeft or .landscapeRight, then we can say that the current orientation is landscape.

Looking forward to your suggestions and comments.

Keep up with us on social media:

LinkedIn : https://www.linkedin.com/company/evangelist-apps-limited/

Twitter : https://twitter.com/EvangelistSW/

--

--