iOS 16 Autorotation strange issue

I am seeing a strange issue with autorotation on iOS 16 that is not seen in other iOS versions. And worse, the issue is NOT seen if I connect device to XCode and debug. It is ONLY seen when I directly launch the app on device once it is installed, and that's the reason I am unable to identify any fix. So here is the summary of the issue. I disable autorotation in the app till the camera session starts running. Once camera session starts running, I fire a notification to force autorotation of device to current orientation.

var disableAutoRotation: Bool {
        if !cameraSessionRunning {
           return true
        }
        return false
  }

 override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    
    var orientations:UIInterfaceOrientationMask = .landscapeRight
    
    if !self.disableAutoRotation {
        orientations = .all
    }
   
    return orientations
 }

 func cameraSessionStartedRunning(_ session:AVCaptureSession?) {
   
    DispatchQueue.main.asyncAfter(deadline: .now(), execute: {
      
        /* 
         * HELP::: This code does something only when debug directly from XCode, 
         * not when directly launching the app on device!!!! 
         */

        cameraSessionRunning = true

        if #available(iOS 16.0, *) {
            UIView.performWithoutAnimation {
                self.setNeedsUpdateOfSupportedInterfaceOrientations()
            }
           
        } else {
            // Fallback on earlier versions
            UIViewController.attemptRotationToDeviceOrientation()
        }
        
        self.layoutInterfaceForOrientation(self.windowOrientation)
       
    })
 }

Dear UIKit Engineers,

I have reproduced the issue with very basic code that disables autorotation on startup and then after 0.2 seconds fires a notification to autorotate. Just create a new project and in ViewController, replace this code. It's shocking that this bug is there in iOS 16. Run this code through XCode debugger while holding iPhone in Portrait mode. and everything runs fine. But if the app is launched directly on iPhone 13 Pro by touching the app icon (and while holding iPhone in portrait mode), it doesn't autorotates to portrait mode (or to be specific, function viewWillTransition() is not called upon autorotation which creates all issues) ! Please do provide workaround as I desperately need it.

I have filed FB11516363 for the same issue.

 import UIKit

class ViewController: UIViewController {

 public var windowOrientation: UIInterfaceOrientation {
     return view.window?.windowScene?.interfaceOrientation ?? .unknown
 }

  private var disableAutoRotation = true

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    
     var orientations:UIInterfaceOrientationMask = .landscapeRight
    
      if !self.disableAutoRotation {
         orientations = .all
      }
   
       return orientations
    }


 override func viewDidLoad() {
     super.viewDidLoad()
    // Do any additional setup after loading the view.
    
      self.view.backgroundColor = UIColor.systemGreen
    
     DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
         self.autoRotateNotification()
     })
 }

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    
    super.viewWillTransition(to: size, with: coordinator)

    let orientation = windowOrientation

        
    coordinator.animate(alongsideTransition: {_ in
    
    }, completion: { [unowned self] (UIViewControllerTransitionCoordinatorContext) -> Void in
        
        let orient = self.windowOrientation
        
        if orient.isLandscape {
            self.view.backgroundColor = UIColor.systemGreen
        } else {
            self.view.backgroundColor = UIColor.systemOrange
        }
       
    })
}

func autoRotateNotification() {
  
   DispatchQueue.main.asyncAfter(deadline: .now(), execute: {
     
       /*
        * HELP::: This code does something only when debug directly from XCode,
        * not when directly launching the app on device!!!!
        */

       self.disableAutoRotation = false

       if #available(iOS 16.0, *) {
           UIView.performWithoutAnimation {
               self.setNeedsUpdateOfSupportedInterfaceOrientations()
           }
          
       } else {
           // Fallback on earlier versions
          UIViewController.attemptRotationToDeviceOrientation()
       }
   })
}


 }

Same Issus , need help

I am facing the same issue. Is there any Apple Engineer here to help us? Thanks

https://developer.apple.com/forums/thread/715052#715052021

@UIKit Engineers

We desperately need your help here. We have to use lot of workarounds(which are not good) to cover up this iOS 16 bug. As of now I tried to introduce a delay of 0.5 seconds to invoke autorotation which works on iPhone 13 pro at the very least. However this introduces an extra latency in app startup and at the same time is not guaranteed to work on every iOS device (particularly the slow ones like iPhone X or older). Please help us.

func autoRotateNotification() {
  
   DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
     
       /*
        * HELP::: This code does something only when debug directly from XCode,
        * not when directly launching the app on device!!!!
        */

       self.disableAutoRotation = false

       if #available(iOS 16.0, *) {
           UIView.performWithoutAnimation {
               self.setNeedsUpdateOfSupportedInterfaceOrientations()
           }
          
       } else {
           // Fallback on earlier versions
          UIViewController.attemptRotationToDeviceOrientation()
       }
   })
}
iOS 16 Autorotation strange issue
 
 
Q