Skip to content

Commit

Permalink
fix: manually enable device orientation notifications (#1543)
Browse files Browse the repository at this point in the history
* fix: call beginGeneratingDeviceOrientationnotifications method

As docs
(https://developer.apple.com/documentation/uikit/uidevice/1620041-begingeneratingdeviceorientation?language=objc)
say: call to this method is necessary to receive correct values from
call to [[UIDevice currentDevice] orientation]. However, even w/o this
fix, it seems to me that call to orienation methods returns correct
value...

* fix: add library installation instructions for iOS

* fix: exclude changes when target OS is TvOS

* chore: update AppDelegate for TestsExample

* fix: AppDelgate for FabricTestExample

* chore: update AppDelegate for FabricExample

* chore: update AppDelegate for Example

* feat: add link to AppDelegate.mm to readme

* fix: add missing dot
  • Loading branch information
kkafar authored Jul 27, 2022
1 parent dcc5ebd commit c6488c4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Example/ios/ScreensExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
InitializeFlipper(application);
#endif

#if !TARGET_OS_TV
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV

RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"ScreensExample"
Expand All @@ -49,6 +53,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
#if !TARGET_OS_TV
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV
}


- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
Expand Down
11 changes: 11 additions & 0 deletions FabricExample/ios/FabricExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
_bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
#endif

#if !TARGET_OS_TV
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV

NSDictionary *initProps = [self prepareInitialProps];
UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"FabricExample", initProps);
Expand All @@ -61,6 +65,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
#if !TARGET_OS_TV
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV
}

/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
///
/// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
Expand Down
10 changes: 10 additions & 0 deletions FabricTestExample/ios/FabricTestExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
_bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
#endif
#if !TARGET_OS_TV
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV

NSDictionary *initProps = [self prepareInitialProps];
UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"FabricTestExample", initProps);
Expand All @@ -60,6 +63,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

- (void)applicationWillTerminate
{
#if !TARGET_OS_TV
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV
}

/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
///
/// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,31 @@ To learn about how to use `react-native-screens` with Fabric architecture, head

### iOS

Installation on iOS should be completely handled with auto-linking, if you have ensured pods are installed after adding this module, no other actions should be necessary.
On iOS obtaining current device orientation [requires asking the system to generate orientation notifications](https://developer.apple.com/documentation/uikit/uidevice/1620053-orientation?language=objc). Our library uses them to enforce correct interface orientation when navigating between screens.
To make sure that there are no issues with screen orientation you should put following code in your `AppDelegate.m`:

```objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
#if !TARGET_OS_TV
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV
...
return YES:
}

- (void)applicationWillTerminate:(UIApplication *)application
{
#if !TARGET_OS_TV
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV
}
```

You can see example of these changes being introduced in our [example applications](https://github.com/software-mansion/react-native-screens/blob/main/TestsExample/ios/TestsExample/AppDelegate.mm).

Other aspects of installation should be completely handled with auto-linking, just ensure you installed pods after adding this module.

### Android

Expand Down
12 changes: 12 additions & 0 deletions TestsExample/ios/TestsExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
_bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
#endif

#if !TARGET_OS_TV
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV

NSDictionary *initProps = [self prepareInitialProps];
UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"TestsExample", initProps);

Expand All @@ -59,6 +64,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
#if !TARGET_OS_TV
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
#endif // !TARGET_OS_TV
}

/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
///
/// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
Expand Down

0 comments on commit c6488c4

Please sign in to comment.