From fd06286e6a5485fd517f7affaa695c0256809c19 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Tue, 13 Dec 2022 10:46:06 -0800 Subject: [PATCH] Always set orientation preferences on iOS 16+ (flutter/engine#38230) --- .../framework/Source/FlutterViewController.mm | 26 ++++++++----------- .../Source/FlutterViewControllerTest.mm | 11 ++++---- 2 files changed, 17 insertions(+), 20 deletions(-) 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 2716ba252e..37b7c1dbb5 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 @@ -1544,21 +1544,17 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) continue; } UIWindowScene* windowScene = (UIWindowScene*)scene; - UIInterfaceOrientationMask currentInterfaceOrientation = - 1 << windowScene.interfaceOrientation; - if (!(_orientationPreferences & currentInterfaceOrientation)) { - [self setNeedsUpdateOfSupportedInterfaceOrientations]; - UIWindowSceneGeometryPreferencesIOS* preference = - [[UIWindowSceneGeometryPreferencesIOS alloc] - initWithInterfaceOrientations:_orientationPreferences]; - [windowScene - requestGeometryUpdateWithPreferences:preference - errorHandler:^(NSError* error) { - os_log_error(OS_LOG_DEFAULT, - "Failed to change device orientation: %@", - error); - }]; - } + UIWindowSceneGeometryPreferencesIOS* preference = + [[UIWindowSceneGeometryPreferencesIOS alloc] + initWithInterfaceOrientations:_orientationPreferences]; + [windowScene + requestGeometryUpdateWithPreferences:preference + errorHandler:^(NSError* error) { + os_log_error(OS_LOG_DEFAULT, + "Failed to change device orientation: %@", + error); + }]; + [self setNeedsUpdateOfSupportedInterfaceOrientations]; } } else { UIInterfaceOrientationMask currentInterfaceOrientation = 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 7ed4c0592e..5cbd2aa81d 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 @@ -891,13 +891,17 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; id mockApplication = OCMClassMock([UIApplication class]); id mockWindowScene; id deviceMock; + FlutterViewController* realVC = [[FlutterViewController alloc] initWithEngine:self.mockEngine + nibName:nil + bundle:nil]; if (@available(iOS 16.0, *)) { mockWindowScene = OCMClassMock([UIWindowScene class]); - OCMStub([mockWindowScene interfaceOrientation]).andReturn(currentOrientation); - if (!didChange) { + if (realVC.supportedInterfaceOrientations == mask) { OCMReject([mockWindowScene requestGeometryUpdateWithPreferences:[OCMArg any] errorHandler:[OCMArg any]]); } else { + // iOS 16 will decide whether to rotate based on the new preference, so always set it + // when it changes. OCMExpect([mockWindowScene requestGeometryUpdateWithPreferences:[OCMArg checkWithBlock:^BOOL( UIWindowSceneGeometryPreferencesIOS* @@ -919,9 +923,6 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; OCMStub([mockApplication sharedApplication]).andReturn(mockApplication); OCMStub([mockApplication statusBarOrientation]).andReturn(currentOrientation); } - FlutterViewController* realVC = [[FlutterViewController alloc] initWithEngine:self.mockEngine - nibName:nil - bundle:nil]; [realVC performOrientationUpdate:mask]; if (@available(iOS 16.0, *)) {