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: manually enable device orientation notifications #1543

Merged
merged 9 commits into from
Jul 27, 2022

Conversation

kkafar
Copy link
Member

@kkafar kkafar commented Jul 25, 2022

Description

According to the Apple docs a call to ... must be made for UIDevice orientation property to work correctly.

I checked and neither we call [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] nor any dependency does this for us (I looked up in node_modules with grep --include=\*.{m,mm} -rnw './node_modules/' -e "beginGeneratingDeviceOrientationNotifications"), however it worked properly. I also checked with generatesDeviceOrientationNotifications method whether orientation notifications were enabled or not. They were on. This nudges me towards conclusion that it possible that these notifications are enabled in some internal Apple code.

However, according to issue #1540 there are some cases when orientation notifications are not enabled "by default", thus we need to manually register for them. As documentation states it is fine to nest calls to the begin... method as long as we also match it with call to the end... method.

Fixes #1540

Changes

  • Updated README.md to include extended installation instructions.

    I recommended adding calls to [[UIDevice currentDevice] {begin,end}GeneratingDeviceOrientationNotifications] to AppDelegate.m to lifecycle methods.

  • Updated AppDelegates in our example applications.

Test code and steps to reproduce

e.g. Test1198 -- navigate to second screen, rotate your device to change interface orientation & go back to first screen -- it's orientation should be correctly adjusted.

Checklist

  • Ensured that CI passes

@kkafar kkafar changed the title @kkafar/ios orientation fix: manually enable device orientation notifications Jul 25, 2022
@kkafar kkafar marked this pull request as ready for review July 25, 2022 11:25
@kkafar kkafar requested a review from WoLewicki July 27, 2022 08:47
@kkafar kkafar merged commit c6488c4 into main Jul 27, 2022
@kkafar kkafar deleted the @kkafar/ios-orientation branch July 27, 2022 09:17
kkafar added a commit that referenced this pull request Nov 18, 2022
## Description

UIKit documentation states that in order to successfully use orientation
in iOS apps the orientation has to be enabled via [the
`beginGeneratingDeviceOrientationNotifications `
method](https://developer.apple.com/documentation/uikit/uidevice/1620041-begingeneratingdeviceorientation?language=objc).
This was addressed in
#1543.


This PR attempts to make this change internal and also removing an
additional `react-native-screens` installation step from README.

PR made based on code from `expo-screen-orientation` package [from this
line](https://github.com/expo/expo/blob/519ac3d86864da9dd920a9ff8d0c880f6ec81a58/ios/versioned/sdk46/EXScreenOrientation/EXScreenOrientation/ABI46_0_0EXScreenOrientationRegistry.m#L38).

Fixes
react-navigation/react-navigation.github.io#1177

Note by @kkafar:

For now I went with placing the code in ctor/dtor of `RNSScreen` view
manager.
The view manager construction & lifecycle is managed by React Native's
internals (and is executed during application startup). This behaviour
is considered stable (changing this would be braking change for RN Paper
architecture).

I considered also:

1. Creating a native module, so we can execute required code in its
lifetime (ctor, dtor) methods -- but it seemed too much... Also we would
have to load this module from JS (prevent lazy loading).
2. Using `dispatch_once` in `+ (voi)enforceDesiredDeviceOrientation`
method - and calling only the `begin...` method (as there would be no
place to call the responding `end...` method.
3. Calling `begin...`/`end...` methods every time
`enforceDesiredDeviceOrientation` is being called.
4. Adding the code to ctor/dtor of `RNSScreenWindowTraits` and creating
a static object. But I had objections. I feared that compiler might
optimise-out (remove) this static variable since it is unused. I've
found some
[mails](https://gcc.gnu.org/pipermail/gcc-help/2021-April/140125.html)
form [gcc-help mailing
list](https://gcc.gnu.org/mailman/listinfo/gcc-help) indicating that it
might be the case.

## Changes

- Moved `beginGeneratingDeviceOrientationNotifications` and
`endGeneratingDeviceOrientationNotifications` methods from applications
`AppDelegate`s to `RNSScreenManager` `init`/`dealloc` methods.

## Testing

I attached a breakpoint to see that added code ends actually being
called.

Orientation playground in `Example/` app.

## Checklist

- [x] Ensured that CI passes

Co-authored-by: Kacper Kafara <[email protected]>
mccoyplayer pushed a commit to mccoyplayer/reactScreen that referenced this pull request Feb 9, 2024
## Description

UIKit documentation states that in order to successfully use orientation
in iOS apps the orientation has to be enabled via [the
`beginGeneratingDeviceOrientationNotifications `
method](https://developer.apple.com/documentation/uikit/uidevice/1620041-begingeneratingdeviceorientation?language=objc).
This was addressed in
software-mansion/react-native-screens#1543.


This PR attempts to make this change internal and also removing an
additional `react-native-screens` installation step from README.

PR made based on code from `expo-screen-orientation` package [from this
line](https://github.com/expo/expo/blob/519ac3d86864da9dd920a9ff8d0c880f6ec81a58/ios/versioned/sdk46/EXScreenOrientation/EXScreenOrientation/ABI46_0_0EXScreenOrientationRegistry.m#L38).

Fixes
react-navigation/react-navigation.github.io#1177

Note by @kkafar:

For now I went with placing the code in ctor/dtor of `RNSScreen` view
manager.
The view manager construction & lifecycle is managed by React Native's
internals (and is executed during application startup). This behaviour
is considered stable (changing this would be braking change for RN Paper
architecture).

I considered also:

1. Creating a native module, so we can execute required code in its
lifetime (ctor, dtor) methods -- but it seemed too much... Also we would
have to load this module from JS (prevent lazy loading).
2. Using `dispatch_once` in `+ (voi)enforceDesiredDeviceOrientation`
method - and calling only the `begin...` method (as there would be no
place to call the responding `end...` method.
3. Calling `begin...`/`end...` methods every time
`enforceDesiredDeviceOrientation` is being called.
4. Adding the code to ctor/dtor of `RNSScreenWindowTraits` and creating
a static object. But I had objections. I feared that compiler might
optimise-out (remove) this static variable since it is unused. I've
found some
[mails](https://gcc.gnu.org/pipermail/gcc-help/2021-April/140125.html)
form [gcc-help mailing
list](https://gcc.gnu.org/mailman/listinfo/gcc-help) indicating that it
might be the case.

## Changes

- Moved `beginGeneratingDeviceOrientationNotifications` and
`endGeneratingDeviceOrientationNotifications` methods from applications
`AppDelegate`s to `RNSScreenManager` `init`/`dealloc` methods.

## Testing

I attached a breakpoint to see that added code ends actually being
called.

Orientation playground in `Example/` app.

## Checklist

- [x] Ensured that CI passes

Co-authored-by: Kacper Kafara <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iOS orientation not being listened to
1 participant