react-native-orientation icon indicating copy to clipboard operation
react-native-orientation copied to clipboard

Orintentation problems with iOS16

Open Girum opened this issue 2 years ago • 25 comments

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

Girum avatar Sep 23 '22 14:09 Girum

Up,same issue

nartco avatar Sep 24 '22 22:09 nartco

+1

bishalfan avatar Sep 25 '22 02:09 bishalfan

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

Girum avatar Sep 26 '22 18:09 Girum

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!

minhpham1203 avatar Sep 28 '22 05:09 minhpham1203

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

Girum avatar Sep 28 '22 11:09 Girum

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

gramiro avatar Sep 30 '22 08:09 gramiro

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 avatar Oct 01 '22 01:10 vinceplusplus

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

The package will be updated?

veerabhadrasajjan avatar Nov 04 '22 13:11 veerabhadrasajjan

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

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

codal-mpawar avatar Nov 10 '22 06:11 codal-mpawar

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

kesha-antonov avatar Nov 22 '22 21:11 kesha-antonov

@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 avatar Nov 23 '22 08:11 gramiro

@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

codal-mpawar avatar Nov 24 '22 07:11 codal-mpawar

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

pawansharmaAccolite avatar Dec 06 '22 12:12 pawansharmaAccolite

@pawansharmaAccolite, yes, it is

vinceplusplus avatar Dec 07 '22 04:12 vinceplusplus

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

pawansharmaAccolite avatar Dec 07 '22 15:12 pawansharmaAccolite

@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

vinceplusplus avatar Dec 08 '22 02:12 vinceplusplus

@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

pawansharmaAccolite avatar Dec 08 '22 04:12 pawansharmaAccolite

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.

codal-mpawar avatar Jan 03 '23 11:01 codal-mpawar

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.

poojasolanki9569 avatar Jan 19 '23 05:01 poojasolanki9569

Any fixes?

nriccar avatar Jan 24 '23 16:01 nriccar

@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.

ou2s avatar Feb 09 '23 10:02 ou2s

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

tipaniclayr30 avatar Feb 24 '23 01:02 tipaniclayr30

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

maoyuhu avatar Apr 23 '23 06:04 maoyuhu

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) {}];
 }

ou2s avatar Apr 26 '23 19:04 ou2s

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(); }; }, []);

taongocson avatar May 16 '24 02:05 taongocson