iOS17.2 change orientation

I have this Objective-C code that works well if placed inside viewDidAppear method - rotates view into the Portrait orientation;

    UIWindowScene *windowScene = self.view.window.windowScene;
    if (!windowScene) { return; }
    UIWindowSceneGeometryPreferences *preferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskPortrait];
    [windowScene requestGeometryUpdateWithPreferences:preferences errorHandler:^(NSError * _Nonnull error) {
        // Handle error here
    }];

Now I need to do that same rotation after user pressed Read button but before the selected document is loaded in another view. The problem - I can't figure out how to force view update after the code is executed. I see initial view rotated only after exiting that selected document, but I need it rotated before entering it.

Thank you

Just found the solution - to push code for the opening of the selected document on the main thread. Now main view rotates before that document is opened:

    dispatch_async(dispatch_get_main_queue(), ^{
        [NSThread sleepForTimeInterval: 0.5];
        [self readIssue:issue];
    });

So were you previously calling requestGeometryU-date… from a non-main thread?

Most of UIKit must be called from the main thread. Did you get any warnings about that? If not, check your build settings to see if you have the UIKit main thread checker enabled.

The previous code stopped working suddenly after I added iOS 15 simulator. Resetted Sonoma and reinstalled Xcode and couldn't make it work again. But it worked once and I decided to add a proper delay. New code is working every time now:

    double delayInSeconds = 1.0; // set the delay time
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self readIssue:issue];
    });
iOS17.2 change orientation
 
 
Q