diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 33fe8c067b6ad..2828ea16814ce 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -618,11 +618,19 @@ - (UIScreen*)mainScreenIfViewLoaded { if (self.viewIfLoaded == nil) { FML_LOG(WARNING) << "Trying to access the view before it is loaded."; } - return self.viewIfLoaded.window.windowScene.screen; + return [self windowSceneIfViewLoaded].screen; } return UIScreen.mainScreen; } +- (UIWindowScene*)windowSceneIfViewLoaded { + if (self.viewIfLoaded == nil) { + FML_LOG(WARNING) << "Trying to access the view before it is loaded."; + return nil; + } + return self.viewIfLoaded.window.windowScene; +} + - (BOOL)loadDefaultSplashScreenView { NSString* launchscreenName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UILaunchStoryboardName"]; @@ -1858,8 +1866,19 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences { [self setNeedsUpdateOfSupportedInterfaceOrientations]; } } else { - UIInterfaceOrientationMask currentInterfaceOrientation = - 1 << [[UIApplication sharedApplication] statusBarOrientation]; + UIInterfaceOrientationMask currentInterfaceOrientation; + if (@available(iOS 13.0, *)) { + UIWindowScene* windowScene = [self windowSceneIfViewLoaded]; + if (!windowScene) { + // When the view is not loaded, it does not make sense to access the interface + // orientation, bail. + FML_LOG(WARNING) << "Trying to access the window scene before the view is loaded."; + return; + } + currentInterfaceOrientation = 1 << windowScene.interfaceOrientation; + } else { + currentInterfaceOrientation = 1 << [[UIApplication sharedApplication] statusBarOrientation]; + } if (!(_orientationPreferences & currentInterfaceOrientation)) { [UIViewController attemptRotationToDeviceOrientation]; // Force orientation switch if the current orientation is not allowed diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index e7b6880c56536..f20e2280ce16c 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -1464,9 +1464,14 @@ - (void)orientationTestWithOrientationUpdate:(UIInterfaceOrientationMask)mask } else { OCMExpect([deviceMock setValue:@(resultingOrientation) forKey:@"orientation"]); } - - OCMStub([mockApplication sharedApplication]).andReturn(mockApplication); - OCMStub([mockApplication statusBarOrientation]).andReturn(currentOrientation); + if (@available(iOS 13.0, *)) { + mockWindowScene = OCMPartialMock(realVC.view.window.windowScene); + OCMStub(((UIWindowScene*)mockWindowScene).interfaceOrientation) + .andReturn(currentOrientation); + } else { + OCMStub([mockApplication sharedApplication]).andReturn(mockApplication); + OCMStub([mockApplication statusBarOrientation]).andReturn(currentOrientation); + } } [realVC performOrientationUpdate:mask]; diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h b/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h index 657c189dc1628..09cb7c455446d 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h @@ -65,6 +65,7 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint); - (void)addInternalPlugins; - (void)deregisterNotifications; - (int32_t)accessibilityFlags; +- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0)); @end