Comments (14)
Nevermind, I thought that the method wasn't even running for some reason but it's definitely running. For some reason my code isn't updating some state like I thought it would. Still, is that the right way to expose an Obj-C method?
from react-native.
Hm, when I do NSLog(@"%x", teapotNode_);
it prints 0. I don't know Obj-C too well, but does it not crash when sending messages to a null pointer? Anyway, it does seem to be calling the method as a static method, not as an instance method. How do I expose an instance method?
EDIT: I'm assuming it's possible because inside RCTUIManager.m
it exports several instance methods. Some of them even reference self
. But I see nothing outside of RCT_EXPORT()
that sets the instance for the exporter...
EDIT2: Oh, I see in initWithJavaScriptExecutor
that it allocates a single instance of the class, so the class is meant to be a single module instance. Might be nice to have a way to register an existing instance as a module?
from react-native.
I don't know Obj-C too well, but does it not crash when sending messages to a null pointer?
No, it doesn't crash; it returns nil.
from react-native.
Ok.
I added a getModule
method on RCTBridge
so now I just do TeapotController *vc = [bridge getModule:@"TeapotController"];
after creating the root view. I'm sure this is not the right way to do it. But I hacked. And it was fun.
from react-native.
I think two things are going on here:
A) Not sure if you're already doing this, but your TeapotController
needs to conform to the RCTNativeModule
protocol in order to be registered with the bridge.
B) If a class conforms to the RCTNativeModule
protocol, it will be instantiated by the RCTBridge
for use in the RCTRootView
. Where do you set the teapotNode_
ivar?
from react-native.
The teapotNode_
is just a scene node object that is created in the viewDidLoad
controller method. I did add that protocol:
@interface TeapotController : GLViewController<RCTNativeModule> {
TeapotNode *teapotNode_;
BOOL tapIsValid_;
CGPoint lastDragPoints_[2];
CGPoint touchStartPoint_;
}
And it's all working fine. The problem is that the bridge instantiates the TeapotController class, but I don't see any way to get to it through the RCTRootView class. I had to add a method myself to dig into the internal modules and return the instance.
from react-native.
To leave a clearer comment about what is needed: we need to be able to access the modules (that are actually class instances) that the bridge creates to have a generic way of talking between iOS and JS. These classes will have state, and when a callback (say from user input) calls back into the iOS world we need access to it.
from react-native.
@jlongster mind updating the title of the issue to reflect what needs to be done here outside of documentation?
from react-native.
Are the discussed items here obsolete? I grep'ed for RCTNativeModule and initWithJavaScriptExecutor across the entire repo and found nothing.
from react-native.
@jaygarcia RCTNativeModule is now called RCTBridgeModule.
from react-native.
Thanks :) I should have replied to my comment. I have been able to grasp things now from the JS <> ObJC side. It wasn't easy, but with enough digging through the ReactKit code, I figured much of what I needed to out.
The Macros are simply genius. I wish the Cordova guys were smart enough to do that.
from react-native.
Hey @jaygarcia , see new guide at https://github.com/facebook/react-native/blob/master/docs/NativeModulesIOS.md. Let us know if you think something should be added there.
from react-native.
Should this be closed?
@frantic , i think the article is pretty air tight for the moment :)
from react-native.
Cool! :)
from react-native.
Related Issues (20)
- GIF images not working after upgrade from React-Native 0.74.3 -> 0.75.1 HOT 5
- React Native: value attribute exists in 0.72 but not in 0.73 HOT 4
- [iOS] `accessibilityElementsHidden` does not work if the parent view is `accessible`
- Some of us hate kotlin, give us java back. HOT 5
- iOS: RCTImageLoader is documented to be thread safe but the setUp code has a race HOT 2
- ccache not found HOT 3
- Error resolving plugin [id: 'com.facebook.react.settings'] HOT 10
- RNGP - Autolinking: Could not find project.android.packageName in react-native config output! HOT 11
- -X is not a valid cp flag, as a result condition always throws exit 1 HOT 4
- buttons not clickable in ios from react native but clickable from react native react-native-gesture-handler HOT 5
- [BUG] - Can't Build project on iOS 15, throws "RCT_ENABLE_INSPECTOR needs to be set to fulfill RCT_REMOTE_PROFILE" HOT 4
- React Native CLI - Error "configureCMakeDebug[arm64-v8a]" [CXX1410]
- The commander dependency is missing in the react-native package HOT 2
- ImageSource is now open for inheritance but the getUri method is still final and cannot override it HOT 3
- App not covering fulll device height HOT 5
- When using TextInput with secureTextEntry={true} on iOS, pressing the back button erases the entire text instead of just the last character. HOT 6
- 'getUri()' cannot override 'getUri()' in 'com.facebook.react.views.imagehelper.ImageSource'; overridden method is final HOT 3
- React Native 0.74.0 - Getting issue on production build -Crashed: Thread: SIGSEGV 0x0000006df6aebf90 HOT 6
- FlatList Items Not Occupying Full Screen Height with Paging Enabled HOT 3
- [Android] ScrollView does not properly handle `testID` property
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native.