Skip to content

Commit 7134021

Browse files
uzegonemadja1ns
authored andcommitted
fix(iOS): Check for multiple screens while changing screen orientation (software-mansion#2035)
## Description This fixes two issues with scenes on iOS: 1. A casting issue, which allows non-window scenes on iOS, e.g. CarPlay's CPTemplateApplicationScene 3. An assumed loading order - the UIWindowScene is not guaranteed to be the first scene (or even present) in a multi-scene app Fixes software-mansion#1857 **Edit:** I originally reported that this would only fix the second crash reported in software-mansion#1857. Upon review of the first crash in that issue, I believe this PR actually addresses both crashes. ## Test code and steps to reproduce There really isn't any simple code that can be used to test this, as this scenario requires having an app that implements multiple scenes, at least one of which being CarPlay. It would be several hundred lines of code. ## Checklist - [x] Ensured that CI passes
1 parent 993c1dc commit 7134021

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

ios/RNSScreenWindowTraits.mm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,21 @@ + (void)enforceDesiredDeviceOrientation
142142
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_16_0
143143
if (@available(iOS 16.0, *)) {
144144
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
145-
UIWindowScene *scene = (UIWindowScene *)array[0];
145+
146+
// when an app supports multiple scenes (e.g. CarPlay), it is possible that
147+
// UIWindowScene is not the first scene, or it may not be present at all
148+
UIWindowScene *scene = nil;
149+
for (id connectedScene in array) {
150+
if ([connectedScene isKindOfClass:[UIWindowScene class]]) {
151+
scene = connectedScene;
152+
break;
153+
}
154+
}
155+
156+
if (scene == nil) {
157+
return;
158+
}
159+
146160
UIWindowSceneGeometryPreferencesIOS *geometryPreferences =
147161
[[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientationMask];
148162
[scene requestGeometryUpdateWithPreferences:geometryPreferences

0 commit comments

Comments
 (0)