Skip to content

Commit

Permalink
fix: properly get window scene to avoid crash on CarPlay app (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
ouabing authored Jun 5, 2024
1 parent 22b3db9 commit 3003526
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions iOS/RCTOrientation/Orientation.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ - (UIInterfaceOrientation)getInterfaceOrientation
{
if(@available(iOS 13, *)) {
UIInterfaceOrientation orientation = UIInterfaceOrientationUnknown;
UIScene *activeScene = [[UIApplication sharedApplication] connectedScenes].allObjects.firstObject;
if ([activeScene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *activeScene = [self getWindowScene];
if (activeScene != nil && [activeScene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *windowScene = (UIWindowScene *)activeScene;
orientation = windowScene.interfaceOrientation;
}
Expand Down Expand Up @@ -111,6 +111,16 @@ - (void)deviceOrientationDidChange:(NSNotification *)notification
}
}

- (UIWindowScene *)getWindowScene {
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
for (id connectedScene in array) {
if ([connectedScene isKindOfClass:[UIWindowScene class]]) {
return connectedScene;
}
}
return nil;
}

- (NSString *)getOrientationStr: (UIInterfaceOrientation)orientation {

NSString *orientationStr;
Expand Down Expand Up @@ -154,17 +164,17 @@ - (void)lockToOrientation:(UIInterfaceOrientation) newOrientation usingMask:(UII
[Orientation setOrientation:mask];

if (@available(iOS 16.0, *)) {
UIWindowScene *windowScene = (UIWindowScene *)[UIApplication sharedApplication].connectedScenes.allObjects.firstObject;

UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:mask];
[windowScene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
UIWindowScene *windowScene = [self getWindowScene];
if (windowScene != nil) {
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:mask];
[windowScene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
#if DEBUG
if (error) {
NSLog(@"Failed to update geometry with UIInterfaceOrientationMask: %@", error);
}
if (error) {
NSLog(@"Failed to update geometry with UIInterfaceOrientationMask: %@", error);
}
#endif
}];

}];
}
} else {
UIDevice* currentDevice = [UIDevice currentDevice];

Expand Down

0 comments on commit 3003526

Please sign in to comment.