Chris
Chris

Reputation: 341

Accessing a React Native module from other native code

I use a native module that works great for getting device info in React Native, in the JS code. I'd like to also make use of it's functionality in other native (Objective-C) code.

Is it possible to access functionality of React Native custom modules from other native code?

Upvotes: 1

Views: 1578

Answers (2)

Sulthan
Sulthan

Reputation: 130072

You can either access the functionality directly (using -[RNDeviceInfo deviceName] method) or using the way React Native is accessing it, that is:

RNDeviceInfo *rn = [[RNDeviceInfo alloc] init];
NSLog(@"Device Name: %@", [rn constantsToExport][@"model"]);

Upvotes: 1

Chris
Chris

Reputation: 341

I found one solution, that is admittedly a bit of a kludge. It does work, but I would imagine this is far from ideal.

At the top of the class that utilizes this code:

@interface RNDeviceInfo ()
- (NSString*) deviceName;
@end

Then I can make use of it like so:

RNDeviceInfo *rn = [[RNDeviceInfo alloc] init];
NSLog(@"Device Name: %@", [rn deviceName]);

Upvotes: 0

Related Questions