diff --git a/packages/react-native/React/CoreModules/RCTPlatform.mm b/packages/react-native/React/CoreModules/RCTPlatform.mm index ec8a5f3220e..2aa4e28be8e 100644 --- a/packages/react-native/React/CoreModules/RCTPlatform.mm +++ b/packages/react-native/React/CoreModules/RCTPlatform.mm @@ -10,6 +10,7 @@ #import #import +#import #import #import @@ -39,10 +40,12 @@ static NSString *interfaceIdiom(UIUserInterfaceIdiom idiom) } } -@interface RCTPlatform () +@interface RCTPlatform () @end -@implementation RCTPlatform +@implementation RCTPlatform { + ModuleConstants _constants; +} RCT_EXPORT_MODULE(PlatformConstants) @@ -51,6 +54,29 @@ RCT_EXPORT_MODULE(PlatformConstants) return YES; } +- (void)initialize +{ + UIDevice *device = [UIDevice currentDevice]; + auto versions = RCTGetReactNativeVersion(); + _constants = typedConstants({ + .forceTouchAvailable = RCTForceTouchAvailable() ? true : false, + .osVersion = [device systemVersion], + .systemName = [device systemName], + .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), + .isTesting = RCTRunningInTestEnvironment() ? true : false, + .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( + {.minor = [versions[@"minor"] doubleValue], + .major = [versions[@"major"] doubleValue], + .patch = [versions[@"patch"] doubleValue], + .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}), +#if TARGET_OS_MACCATALYST + .isMacCatalyst = true, +#else + .isMacCatalyst = false, +#endif + }); +} + - (dispatch_queue_t)methodQueue { return dispatch_get_main_queue(); @@ -59,35 +85,12 @@ RCT_EXPORT_MODULE(PlatformConstants) // TODO: Use the generated struct return type. - (ModuleConstants)constantsToExport { - return (ModuleConstants)[self getConstants]; + return _constants; } - (ModuleConstants)getConstants { - __block ModuleConstants constants; - RCTUnsafeExecuteOnMainQueueSync(^{ - UIDevice *device = [UIDevice currentDevice]; - auto versions = RCTGetReactNativeVersion(); - constants = typedConstants({ - .forceTouchAvailable = RCTForceTouchAvailable() ? true : false, - .osVersion = [device systemVersion], - .systemName = [device systemName], - .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), - .isTesting = RCTRunningInTestEnvironment() ? true : false, - .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( - {.minor = [versions[@"minor"] doubleValue], - .major = [versions[@"major"] doubleValue], - .patch = [versions[@"patch"] doubleValue], - .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}), -#if TARGET_OS_MACCATALYST - .isMacCatalyst = true, -#else - .isMacCatalyst = false, -#endif - }); - }); - - return constants; + return _constants; } - (std::shared_ptr)getTurboModule:(const ObjCTurboModule::InitParams &)params diff --git a/packages/react-native/package.json b/packages/react-native/package.json index 6fdcfa48049..63ca1cb983d 100644 --- a/packages/react-native/package.json +++ b/packages/react-native/package.json @@ -149,7 +149,13 @@ { "name": "FBReactNativeSpec", "type": "modules", - "ios": {}, + "ios": { + "modules": { + "PlatformConstants": { + "unstableRequiresMainQueueSetup": true + } + } + }, "android": {}, "jsSrcsDir": "src" },