Determining Landscape Left and Landscape Right?

I'm curious if anyone has discovered a way to determining if their Messages app is in landscape left or landscape right? I've seen this topic come up in other discussions, but have not seen a resolution. Since Messages Extensions do support use of the camera and AVFoundation, I've been unable to set my video orientation as I'd usually use UIDevice.current.orientation to determine the orientation. Messages Extensions consistently report an unknown orientation, rather than Face Up, Face Down, Portrait, Landscape Left, Landscape Right, etc.


I've been able to use a helpful suggestion of someone here to determine portrait vs. landscape by checking the following in my viewDidLayoutSubviews();


if UIScreen.main.bounds.size.width < UIScreen.main.bounds.size.height {
     // Portrait
} else {
     // Landscape
}


This, however, results in things working well in portrait, but can result in rotated or upside down images in landscape since I cannot set landscape left or landscape right (or portrait upside down on iPad, for that matter).


Thanks!

Try this:


CGPoint fixedPoint = [self.view convertPoint:CGPointMake(1.0, 1.0) toCoordinateSpace:[self.view.window.screen fixedCoordinateSpace]];
UIDeviceOrientation orientation = UIDeviceOrientationUnknown;
if (fixedPoint.x > 0.0) {
     if (fixedPoint.y > 0.0)
          orientation = UIDeviceOrientationPortrait;
     else
          orientation = UIDeviceOrientationLandscapeRight;
}
else {
     if (fixedPoint.y > 0.0)
          orientation = UIDeviceOrientationLandscapeLeft;
     else
          orientation = UIDeviceOrientationPortraitUpsideDown;
}


The idea is that a point within a view will have different locations in the fixed coordinate space of the screen depending on the device rotation. It seems to work OK in my extension.


Edit: just to clarify, this code is placed in the view controller's viewWillTransitionToSize:withTransitionCoordinator: method.

A better version based on screen coordinates:


- (UIDeviceOrientation)orientation {
  CGPoint fixedPoint = [self.view.window.screen.coordinateSpace convertPoint:CGPointMake(0.0, 0.0) toCoordinateSpace:self.view.window.screen.fixedCoordinateSpace];

  if (fixedPoint.x == 0.0) {
       if (fixedPoint.y == 0.0)
            return UIDeviceOrientationPortrait;
       else
            return UIDeviceOrientationLandscapeRight;
  }
  else {
       if (fixedPoint.y == 0.0)
            return UIDeviceOrientationLandscapeLeft;
       else
            return UIDeviceOrientationPortraitUpsideDown;
  }
}

extension UIViewController {

    var deviceOrientation:UIDeviceOrientation {

        guard let window = view.window else { return .unknown }

        let fixedPoint = window.screen.coordinateSpace.convert(CGPoint.zero, to: window.screen.fixedCoordinateSpace)

        if fixedPoint.x == 0 {

            if fixedPoint.y == 0 { return .portrait }

            return .landscapeRight

        } else {

            if fixedPoint.y == 0 { return .landscapeLeft }

            return .portraitUpsideDown

        }

    }

}

Determining Landscape Left and Landscape Right?
 
 
Q