From 920867d9494cbfcc9cb0e23607cb339ec1b89ca9 Mon Sep 17 00:00:00 2001 From: TobiasH Date: Tue, 31 Dec 2024 07:49:36 -0800 Subject: [PATCH] Fix useWindowDimensions not updating because of delayed applicationState update on iOS devices (#46353) Summary: ### Issue When a real device is oriented into landscape and the user locks the screen during said orientation incase the user rotates back to previous orientation and unlocks the screen `useWindowDimensions` will not get the correctly updated values. This is due to `applicationState` being equal to UIApplicationStateInactive still when `interfaceFrameDidChange` gets called. ### Fix `didUpdateDimensions` on iOS. Now correctly emits the dimension values after the device has been oriented and device has been locked. By adding `UIDeviceOrientationDidChangeNotification` to `NSNotificationCenter` ## Changelog: [IOS] [FIXED] - Emit didUpdateDimensions correctly ``` // RCTDeviceInfo.mm // Adds the interfaceFrameDidChange to UIDeviceOrientationDidChangeNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:selector(interfaceFrameDidChange) name:UIDeviceOrientationDidChangeNotification object:nil]; ``` Pull Request resolved: https://github.com/facebook/react-native/pull/46353 Test Plan: ### ***Note***: This doesn't seem to be replicable on simulators. It only happens on real iOS devices. ### Before change: Rotate Device > Lock Screen > Rotate back to portrait > Unlock phone ![IMG_6089](https://github.com/user-attachments/assets/5d928613-2742-45fb-97fa-d87eaf64ea97) ### After change: Same steps as above, now emits correct values ![IMG_6091](https://github.com/user-attachments/assets/eb46ed22-c3c5-4e77-8069-4a604a21947e) Reviewed By: cortinico Differential Revision: D67735523 Pulled By: cipolleschi fbshipit-source-id: 146e5d62d55eeef0f6b17f962ca84ab418a7b7f0 --- packages/react-native/React/CoreModules/RCTDeviceInfo.mm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 648504cd1a2..95395e190cf 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -68,6 +68,10 @@ RCT_EXPORT_MODULE() selector:@selector(interfaceFrameDidChange) name:RCTWindowFrameDidChangeNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(interfaceFrameDidChange) + name:UIApplicationDidBecomeActiveNotification + object:nil]; #if TARGET_OS_IOS