yamill/react-native-orientation

Orintentation problems with iOS16

Opened this issue · 25 comments

Girum commented

Hi! After update to iOS16, some functionallities dont work sometimes. The package will be updated?

Up,same issue

Girum commented

I changed to react-native-orientation-locker and it works fine! But now android has a problem: I'm using react-native-video lib. When I change the orientation, or even change something, the video reloads

I changed to react-native-orientation-locker and it works fine! But now android has a problem: I'm using react-native-video lib. When I change the orientation, or even change something, the video reloads
Hi @Girum , I am using react-native-orientation-locker, but the callback function is not called in addOrientationListener. So I can not check the orientation. Any solution? Thank you!

Girum commented

Hi @minhpham1203 . HEre works perfectly.

Orientation.addOrientationListener(orientationChange)
        
return () => {
  Orientation.removeOrientationListener(orientationChange)
};
const orientationChange = (orientation) => {
        if(orientation == 'LANDSCAPE-RIGHT' || orientation ==  'LANDSCAPE-LEFT') {
           ...

            
        } else if (orientation == 'PORTRAIT') {
            ...
        }
    }

The only change I made is that dont exists the enum "LANDASCAPE". I had to set the conditions to "LANDASCAPE_LEFT" and "LANDASCAPE_RIGHT"

@minhpham1203 afeter another test, it not works too... =(. We have to ask in the react-native-orientation-locker github

It fires the if(orientation == 'LANDSCAPE-RIGHT' || orientation == 'LANDSCAPE-LEFT'), but not work in another functions inside of this if

Hey guys, here's a PR with a fix for iOS16 orientation :)

if you are still building with iOS 15 sdk, could try going into Orientation.m, and add

[UIViewController attemptRotationToDeviceOrientation];

below every

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: ... ] forKey:@"orientation"];

that should work on iOS 16

Hey guys, here's a PR with a fix for iOS16 orientation :)

The package will be updated?

Hey guys, here's a PR with a fix for iOS16 orientation :)

any update on this? @gramiro @vinceplusplus @Girum @veerabhadrasajjan

I think we need to take ownership of this package or use some stable fork since this lib wasn't updated for 5 years. I think it's unlikely it'll be updated in the future

@codal-mpawar I'm not the owner of the library so I guess we need to do what @kesha-antonov is proposing. As a temporary solution I'm just patching my projects.

@gramiro it's working locally but it's not working on the release mode.

below is patch package.
diff --git a/node_modules/react-native-orientation/iOS/RCTOrientation/Orientation.m b/node_modules/react-native-orientation/iOS/RCTOrientation/Orientation.m
index 15e9927..67940a4 100644
--- a/node_modules/react-native-orientation/iOS/RCTOrientation/Orientation.m
+++ b/node_modules/react-native-orientation/iOS/RCTOrientation/Orientation.m
@@ -39,15 +39,38 @@ + (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, *)) {
    +#ifdef __IPHONE_16_0
  •    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];
    

+#endif

  • } 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 {
    @@ -156,12 +179,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)
@@ -172,17 +191,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];
    
    }
    }

@@ -192,11 +205,7 @@ - (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)
@@ -204,13 +213,8 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
#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)
@@ -218,9 +222,9 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
#if DEBUG
NSLog(@"Unlock All Orientations");
#endif

  • UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    [Orientation setOrientation:UIInterfaceOrientationMaskAllButUpsideDown];
    -// AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    -// delegate.orientation = 3;
  • [self lockToOrientationWithMask:UIInterfaceOrientationMaskAllButUpsideDown interfaceOrientation:UIInterfaceOrientationMaskAllButUpsideDown deviceOrientation:orientation];
    }
  • (NSDictionary *)constantsToExport

if you are still building with iOS 15 sdk, could try going into Orientation.m, and add

[UIViewController attemptRotationToDeviceOrientation];

below every

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: ... ] forKey:@"orientation"];

that should work on iOS 16

can you confirm that is working in real device not in simulator

yes same here i made this patch its working fine at local but when i hit on pipeline its not working

@pawansharmaAccolite if it’s working locally probably you haven’t had the patch applied on the pipeline. you will need to either fork this repo and add the change or use something like https://www.npmjs.com/package/patch-package to check in locally applied patches to your app repo. Also if it’s the fix I mentioned, i think it will only work if you are building with Xcode 14/iOS 15 sdk, so make sure your pipeline is using the same setup

@pawansharmaAccolite if it’s working locally probably you haven’t had the patch applied on the pipeline. you will need to either fork this repo and add the change or use something like https://www.npmjs.com/package/patch-package to check in locally applied patches to your app repo. Also if it’s the fix I mentioned, i think it will only work if you are building with Xcode 14/iOS 15 sdk, so make sure your pipeline is using the same setup

yes i face same issue in pipeline
but guys i am okay with that it does not work on ios 16 but it should work on ios 14 and 15 because. my pipeline using xcode 13.1 to deploy but still i don't understand why its not working in real device
i don't have access of real device which have ios 14 or 15. Only device i have is ios 16

that's why i asked for help that its really working in real device which have ios 14 or 15

if you are still building with iOS 15 sdk, could try going into Orientation.m, and add

[UIViewController attemptRotationToDeviceOrientation];

below every

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: ... ] forKey:@"orientation"];

that should work on iOS 16

@vinceplusplus Can you please share more info because i added with this way and its not working on released mode.

Pipeline should have the same setup (macOS version (13.1) and xcode (14.2)), then it is working fine locally as well as on real devices.

Any fixes?

ou2s commented

@codal-mpawar thanks for the fix.

FYI, this line causes occasional crashes :
UIWindowScene *scene = (UIWindowScene *)array[0];

Crash:
*** -[__NSArrayI_Transfer objectAtIndexedSubscript:]: index 0 beyond bounds for empty array

I think it's because I call this function when the app is still initialising.

do you already have a solution for this?
Im also experiencing it huhuhu

hey,guys,i have solved the problem for iOS 16 system,and it work fine in ios 16 system. in Orientation.m i add this code

1.0
//适配iOS16系统
-(void)adapterIOS16SystemWithOrientation:(UIInterfaceOrientationMask)Orientation{
if (@available(iOS 16.0, *)) {
UIWindow *originalKeyWindow = [[UIApplication sharedApplication] keyWindow];
if (originalKeyWindow != nil) {
[originalKeyWindow.rootViewController setNeedsUpdateOfSupportedInterfaceOrientations];
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = (UIWindowScene *)array[0];
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:Orientation];
[scene requestGeometryUpdateWithPreferences:geometryPreferences
errorHandler:^(NSError * _Nonnull error) {
NSLog(@"error---%@", error.description);
}];
}
}
}

2.0
and call where needed ,for example:
RCT_EXPORT_METHOD(lockToLandscapeRight)
{
#if DEBUG
NSLog(@"Locked to Landscape Right");
#endif
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeRight];

  //    // this seems counter intuitive
      [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
      [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    [self adapterIOS16SystemWithOrientation:UIInterfaceOrientationMaskLandscapeRight];

}];
}
3.0 it work fine good luck

ou2s commented

do you already have a solution for this? Im also experiencing it huhuhu

@tipaniclayr30 Sorry you didn't tag me, so I didn't see your question. I just added a safeguard:

NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
if (array.count > 0) {
      UIWindowScene *scene = (UIWindowScene *)array[0];
      [UIViewController attemptRotationToDeviceOrientation];
       UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:maskOrientation];
       [scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {}];
 }

I've added: react-native-orientation-locker
then in file App.js
import Orientation from "react-native-orientation-locker";

useEffect(() => { Orientation.lockToPortrait(); return () => { Orientation.unlockAllOrientations(); }; }, []);