Skip to content

Commit

Permalink
fix(iOS): Check for multiple screens while changing screen orientation (
Browse files Browse the repository at this point in the history
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
  • Loading branch information
uzegonemad authored and ja1ns committed Oct 9, 2024
1 parent 993c1dc commit 7134021
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ios/RNSScreenWindowTraits.mm
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,21 @@ + (void)enforceDesiredDeviceOrientation
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_16_0
if (@available(iOS 16.0, *)) {
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = (UIWindowScene *)array[0];

// when an app supports multiple scenes (e.g. CarPlay), it is possible that
// UIWindowScene is not the first scene, or it may not be present at all
UIWindowScene *scene = nil;
for (id connectedScene in array) {
if ([connectedScene isKindOfClass:[UIWindowScene class]]) {
scene = connectedScene;
break;
}
}

if (scene == nil) {
return;
}

UIWindowSceneGeometryPreferencesIOS *geometryPreferences =
[[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientationMask];
[scene requestGeometryUpdateWithPreferences:geometryPreferences
Expand Down

0 comments on commit 7134021

Please sign in to comment.