iosuideviceorientation

How to detect iOS device orientation from background?


I am working on an app for a client that runs in the background.

It's not an app store app, so it does not have quite the same rules that normally apply to app store apps. Even private APIs are an option for this app, but it must run on a non-jailbroken device. (The app will be an enterprise app.)

The app needs to know the current device orientation. Seems simple, but it's not. I'm using the normal code you'd expect:

UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
BOOL inLandscape = UIDeviceOrientationIsLandscape(deviceOrientation);

However, it seems that this returns the orientation that was in force when the app moved from foreground to background.

I need the orientation to track the actual device orientation. Actually what I REALLY want is the user interface orientation of the current foreground app, but Apple makes that really hard to deduce. I'll settle for the device orientation if I have to.

This app is written in Objective-C, but I "speak Swift" as well, so answers expressed in either language are fine.


Solution

  • There is a private method called -[UIApplication _getSpringBoardOrientation] which just calls another private method -[UIApplication activeInterfaceOrientation], which seems to give you the orientation of the current foreground app.