Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9bd30c3

Browse files
author
Chris Yang
authored
[ios] use interfaceOrientation orientation on iOS 13 and above (#42846)
The UIApplication.statusBarOrientation is deprecated on iOS 9. The replacement is UIScene.interfaceOrientation, which is available on iOS 13 and above. See: https://developer.apple.com/documentation/uikit/uiapplication/1623026-statusbarorientation?language=objc Part of flutter/flutter#128730 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 041998f commit 9bd30c3

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

shell/platform/darwin/ios/framework/Source/FlutterViewController.mm

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,19 @@ - (UIScreen*)mainScreenIfViewLoaded {
618618
if (self.viewIfLoaded == nil) {
619619
FML_LOG(WARNING) << "Trying to access the view before it is loaded.";
620620
}
621-
return self.viewIfLoaded.window.windowScene.screen;
621+
return [self windowSceneIfViewLoaded].screen;
622622
}
623623
return UIScreen.mainScreen;
624624
}
625625

626+
- (UIWindowScene*)windowSceneIfViewLoaded {
627+
if (self.viewIfLoaded == nil) {
628+
FML_LOG(WARNING) << "Trying to access the view before it is loaded.";
629+
return nil;
630+
}
631+
return self.viewIfLoaded.window.windowScene;
632+
}
633+
626634
- (BOOL)loadDefaultSplashScreenView {
627635
NSString* launchscreenName =
628636
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"UILaunchStoryboardName"];
@@ -1858,8 +1866,19 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences {
18581866
[self setNeedsUpdateOfSupportedInterfaceOrientations];
18591867
}
18601868
} else {
1861-
UIInterfaceOrientationMask currentInterfaceOrientation =
1862-
1 << [[UIApplication sharedApplication] statusBarOrientation];
1869+
UIInterfaceOrientationMask currentInterfaceOrientation;
1870+
if (@available(iOS 13.0, *)) {
1871+
UIWindowScene* windowScene = [self windowSceneIfViewLoaded];
1872+
if (!windowScene) {
1873+
// When the view is not loaded, it does not make sense to access the interface
1874+
// orientation, bail.
1875+
FML_LOG(WARNING) << "Trying to access the window scene before the view is loaded.";
1876+
return;
1877+
}
1878+
currentInterfaceOrientation = 1 << windowScene.interfaceOrientation;
1879+
} else {
1880+
currentInterfaceOrientation = 1 << [[UIApplication sharedApplication] statusBarOrientation];
1881+
}
18631882
if (!(_orientationPreferences & currentInterfaceOrientation)) {
18641883
[UIViewController attemptRotationToDeviceOrientation];
18651884
// Force orientation switch if the current orientation is not allowed

shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,9 +1464,14 @@ - (void)orientationTestWithOrientationUpdate:(UIInterfaceOrientationMask)mask
14641464
} else {
14651465
OCMExpect([deviceMock setValue:@(resultingOrientation) forKey:@"orientation"]);
14661466
}
1467-
1468-
OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
1469-
OCMStub([mockApplication statusBarOrientation]).andReturn(currentOrientation);
1467+
if (@available(iOS 13.0, *)) {
1468+
mockWindowScene = OCMPartialMock(realVC.view.window.windowScene);
1469+
OCMStub(((UIWindowScene*)mockWindowScene).interfaceOrientation)
1470+
.andReturn(currentOrientation);
1471+
} else {
1472+
OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
1473+
OCMStub([mockApplication statusBarOrientation]).andReturn(currentOrientation);
1474+
}
14701475
}
14711476

14721477
[realVC performOrientationUpdate:mask];

shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint);
6565
- (void)addInternalPlugins;
6666
- (void)deregisterNotifications;
6767
- (int32_t)accessibilityFlags;
68+
- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0));
6869

6970
@end
7071

0 commit comments

Comments
 (0)