How to determine the orientation of an iOS device in the background?

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

It is not an application for applications for applications, therefore, it does not have exactly the same rules that usually apply to applications for application applications. Even private APIs are an option for this application, but it should work on a non-jailbroken device. (The application will be a corporate application.)

The application must know the current orientation of the device. It seems simple, but it is not. I am using the regular code that you expect:

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

However, it seems that this returns the orientation that was valid when the application moved from the foreground to the background.

I need orientation to track the actual orientation of the device. In fact, what I really want is the user interface orientation of the current foreground application, but Apple makes it very difficult to output. I will agree to the orientation of the device if I need to.

This application is written in Objective-C, but I also say "Swift", so the answers expressed in any language are beautiful.

+4
source share
1 answer

There is a private method called -[UIApplication _getSpringBoardOrientation]that simply calls another private method -[UIApplication activeInterfaceOrientation], which appears to give you the orientation of the current foreground application.

+6
source

Source: https://habr.com/ru/post/1607491/


All Articles