diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 33fe8c067b..2828ea1681 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -618,11 +618,19 @@ static void SendFakeTouchEvent(FlutterEngine* engine, 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 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) [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/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index e7b6880c56..f20e2280ce 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -1464,9 +1464,14 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; } 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/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h index 657c189dc1..09cb7c4554 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h +++ b/engine/src/flutter/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