[ios] use interfaceOrientation orientation on iOS 13 and above (flutter/engine#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 https://github.com/flutter/flutter/issues/128730

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Chris Yang
2023-06-14 15:33:07 -07:00
committed by GitHub
parent 7f616ca95a
commit 57582c3dd0
3 changed files with 31 additions and 6 deletions

View File

@@ -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

View File

@@ -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];

View File

@@ -65,6 +65,7 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint);
- (void)addInternalPlugins;
- (void)deregisterNotifications;
- (int32_t)accessibilityFlags;
- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0));
@end