Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: orientation on iOS16 #413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions iOS/RCTOrientation/Orientation.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,36 @@ + (BOOL)requiresMainQueueSetup
return YES;
}

- (void)dispatchOrientationChangeEvent:(UIDeviceOrientation)orientation {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"specificOrientationDidChange"
body:@{@"specificOrientation": [self getSpecificOrientationStr:orientation]}];

[self.bridge.eventDispatcher sendDeviceEventWithName:@"orientationDidChange"
body:@{@"orientation": [self getOrientationStr:orientation]}];
}

- (void)lockToOrientationWithMask:(UIInterfaceOrientationMask)maskOrientation interfaceOrientation:(UIInterfaceOrientation)interfaceOrientation deviceOrientation:(UIDeviceOrientation)deviceOrientation {
if (@available(iOS 16, *)) {
dispatch_sync(dispatch_get_main_queue(), ^{
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = (UIWindowScene *)array[0];
[UIViewController attemptRotationToDeviceOrientation];
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:maskOrientation];
[scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {}];
});
[self dispatchOrientationChangeEvent:deviceOrientation];
} else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:interfaceOrientation] forKey:@"orientation"];
}];
}
}

- (void)deviceOrientationDidChange:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"specificOrientationDidChange"
body:@{@"specificOrientation": [self getSpecificOrientationStr:orientation]}];

[self.bridge.eventDispatcher sendDeviceEventWithName:@"orientationDidChange"
body:@{@"orientation": [self getOrientationStr:orientation]}];

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self dispatchOrientationChangeEvent:orientation];
}

- (NSString *)getOrientationStr: (UIDeviceOrientation)orientation {
Expand Down Expand Up @@ -156,12 +177,8 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
#if DEBUG
NSLog(@"Locked to Portrait");
#endif
[Orientation setOrientation:UIInterfaceOrientationMaskPortrait];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
}];

[Orientation setOrientation:UIInterfaceOrientationMaskPortrait];
[self lockToOrientationWithMask:UIInterfaceOrientationMaskPortrait interfaceOrientation:UIInterfaceOrientationPortrait deviceOrientation:UIDeviceOrientationPortrait];
}

RCT_EXPORT_METHOD(lockToLandscape)
Expand All @@ -172,17 +189,11 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *orientationStr = [self getSpecificOrientationStr:orientation];
if ([orientationStr isEqualToString:@"LANDSCAPE-LEFT"]) {
[Orientation setOrientation:UIInterfaceOrientationMaskLandscape];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}];
[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeRight];
[self lockToOrientationWithMask:UIInterfaceOrientationMaskLandscapeRight interfaceOrientation:UIInterfaceOrientationLandscapeRight deviceOrientation:UIDeviceOrientationLandscapeRight];
} else {
[Orientation setOrientation:UIInterfaceOrientationMaskLandscape];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
}];
[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeLeft];
[self lockToOrientationWithMask:UIInterfaceOrientationMaskLandscapeLeft interfaceOrientation:UIInterfaceOrientationLandscapeLeft deviceOrientation:UIDeviceOrientationLandscapeLeft];
}
}

Expand All @@ -192,25 +203,16 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
NSLog(@"Locked to Landscape Left");
#endif
[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeLeft];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
}];

[self lockToOrientationWithMask:UIInterfaceOrientationMaskLandscapeLeft interfaceOrientation:UIInterfaceOrientationLandscapeLeft deviceOrientation:UIDeviceOrientationLandscapeLeft];
}

RCT_EXPORT_METHOD(lockToLandscapeRight)
{
#if DEBUG
NSLog(@"Locked to Landscape Right");
#endif
[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeRight];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
// this seems counter intuitive
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}];

[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeRight];
[self lockToOrientationWithMask:UIInterfaceOrientationMaskLandscapeRight interfaceOrientation:UIInterfaceOrientationLandscapeRight deviceOrientation:UIDeviceOrientationLandscapeRight];
}

RCT_EXPORT_METHOD(unlockAllOrientations)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to update unlockAllOrientations to -

RCT_EXPORT_METHOD(unlockAllOrientations)
{
  #if DEBUG
    NSLog(@"Unlock All Orientations");
  #endif
  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  [Orientation setOrientation:UIInterfaceOrientationMaskAllButUpsideDown];
  [self lockToOrientationWithMask:UIInterfaceOrientationMaskAllButUpsideDown interfaceOrientation:UIInterfaceOrientationMaskAllButUpsideDown deviceOrientation:orientation];
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gramiro - Any update?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, guys please update ASAP. we are stuck here for the build.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi guys, sorry, been a while. @QSuraj I didn't include this since it was not in the original library

Expand Down