You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/the-new-architecture/pure-cxx-modules.md
+77-28Lines changed: 77 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -318,46 +318,95 @@ If you did everything right, your project on the left should look like this:
318
318
319
319
#### 3. Registering the Cxx Turbo Native Module in your app
320
320
321
-
:::warning
322
-
If your app has some local modules that are written in C++, you would not be able to use the AppDelegate in Swift that we shipped in React Native 0.77.
321
+
To register a pure Cxx Turbo Native Module in your app, you need to:
323
322
324
-
If your app falls in this category, please skip the migration of the AppDelegate to Swift, and keep using Objective-C++ for your app's AppDelegate.
323
+
1. Create a `ModuleProvider` for the Native Module
324
+
2. Configure the `package.json` to associate the JS module name with the ModuleProvider class.
325
325
326
-
React Native core is mostly developed using C++ to encourage code sharing between iOS and Android and other platforms. The interoperability between Swift and C++ is not mature nor stable, yet. We are looking into ways to fill this gap and let you migrate to Swift too.
327
-
:::
326
+
The ModuleProvider is an Objective-C++ that glues together the Pure C++ module with the rest of your iOS App.
328
327
329
-
With this last step, we will tell the iOS app where to look for to find the pure C++ Turbo Native Module.
328
+
##### 3.1 Create the ModuleProvider
330
329
331
-
In Xcode, open the `AppDelegate.mm` file and modify it as follows:
330
+
1. From Xcode, select the `SampleApp` project and press <kbd>⌘</kbd> + <kbd>N</kbd> to create a new file.
331
+
2. Select the `Cocoa Touch Class` template
332
+
3. Add the name `SampleNativeModuleProvider` (keep the other field as `Subclass of: NSObject` and `Language: Objective-C`)
333
+
4. Click Next to generate the files.
334
+
5. Rename the `SampleNativeModuleProvider.m` to `SampleNativeModuleProvider.mm`. The `mm` extension denotes an Objective-C++ file.
335
+
6. Implement the content of the `SampleNativeModuleProvider.h` with the following:
This declares a `NativeSampleModuleProvider` object that conforms to the `RCTModuleProvider` protocol.
357
352
358
-
1. Importing the `RCTAppDelegate+Protected` header to make it visible to the AppDelegate that it is conforming to the `RCTTurboModuleManagerDelegate` protocol.
359
-
2. Importing the Pure C++ Native Turbo Module interface `NativeSampleModule.h`
360
-
3. Overriding the `getTurboModule` method for C++ modules so that when the JS side asks for a module called `NativeSampleModule`, the app knows which module has to be returned.
353
+
7. Implement the content of the `SampleNativeModuleProvider.mm` with the following:
This code implements the `RCTModuleProvider` protocol by creating the pure C++ `NativeSampleModule` when the `getTurboModule:` method is called.
374
+
375
+
##### 3.2 Update the package.json
376
+
377
+
The last step consist in updating the `package.json` to tell React Native about the link between the JS specs of the Native Module and the concrete implementation of those spec in native code.
@@ -74,6 +77,7 @@ You can add this snippet to your app and customize the various fields:
74
77
-`ios.modulesConformingToProtocol.RCTURLRequestHandler`: list of iOS native module that implements the [`RCTURLRequestHandler` protocol](https://github.com/facebook/react-native/blob/00d5caee9921b6c10be8f7d5b3903c6afe8dbefa/packages/react-native/React/Base/RCTURLRequestHandler.h#L11-L52). You need to pass the class names of iOS classes that implements the `RCTURLRequestHandler`. They must be Native Modules.
75
78
-`ios.modulesConformingToProtocol.RCTImageDataDecoder`: list of iOS native module that implements the [`RCTImageDataDecoder` protocol](https://github.com/facebook/react-native/blob/00d5caee9921b6c10be8f7d5b3903c6afe8dbefa/packages/react-native/Libraries/Image/RCTImageDataDecoder.h#L15-L53). You need to pass the class names of iOS classes that implements the `RCTImageDataDecoder`. They must be Native Modules.
76
79
-`ios.componentProvider`: this field is a map used to generate the association between a custom JS React component and the native class that implements it. The key of the map is the JS name of the component (for example `TextInput`), and the value is the iOS class that implements the component (for example `RCTTextInput`).
80
+
-`ios.modulesProvider`: this field is a map used to generate the association between a custom JS Native Module and the native class that can provide it. The key of the map is the JS name of the module (for example `NativeLocalStorage`), and the value is the iOS class that implements the [`RCTModuleProvider` protocol](https://github.com/facebook/react-native/blob/0.79-stable/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h#L179-L190). For Objective-C modules, the class implementing the [`RCTTurboModule` protocol](https://github.com/facebook/react-native/blob/0.79-stable/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h#L192-L200) is also implementing the `RCTModuleProvider` protocol. For more information, looks at the [Cross-Platform Native Modules (C++) guide](/docs/next/the-new-architecture/pure-cxx-modules).
77
81
78
82
When **Codegen** runs, it searches among all the dependencies of the app, looking for JS files that respects some specific conventions, and it generates the required code:
- `RCT_EXPORT_MODULE` exports and registers the module using the identifier we'll use to access it in the JavaScript environment: `NativeLocalStorage`. See these [docs](./legacy/native-modules-ios#module-name) for more details.
120
122
- You can use Xcode to jump to the Codegen `@protocol NativeLocalStorageSpec`. You can also use Xcode to generate stubs for you.
121
123
124
+
## Register the Native Module in your app
125
+
126
+
The last step consist in updating the `package.json` to tell React Native about the link between the JS specs of the Native Module and the concrete implementation of those specs in native code.
127
+
128
+
Modify the `package.json` as it follows:
129
+
130
+
```json title="package.json"
131
+
"start": "react-native start",
132
+
"test": "jest"
133
+
},
134
+
"codegenConfig": {
135
+
"name": "AppSpecs",
136
+
"type": "modules",
137
+
"jsSrcsDir": "specs",
138
+
"android": {
139
+
"javaPackageName": "com.sampleapp.specs"
140
+
}
141
+
// highlight-add-start
142
+
"ios":
143
+
"modulesProvider": {
144
+
"NativeLocalStorage": "RCTNativeLocalStorage"
145
+
}
146
+
// highlight-add-end
147
+
},
148
+
149
+
"dependencies": {
150
+
```
151
+
152
+
At this point, you need to re-install the pods to make sure that codegen runs again to generate the new files:
153
+
154
+
```bash
155
+
# from the ios folder
156
+
bundle exec pod install
157
+
open SampleApp.xcworkspace
158
+
```
159
+
160
+
If you now build your application from Xcode, you should be able to build successfully.
0 commit comments