在 iOS 库中启用
本文档仍为 experimental,详细信息可能会随着我们的迭代而发生变化。 欢迎在此页面分享你对 工作组内部讨论 的反馈。
英This documentation is still experimental and details are subject to changes as we iterate. Feel free to share your feedback on the discussion inside the working group for this page.
而且,它还包含几个 手动步骤。 请注意,一旦新架构稳定,这将不代表最终的开发者体验。 我们正在开发工具、模板和库,以帮助你快速开始使用新架构,而无需完成整个设置。
英Moreover, it contains several manual steps. Please note that this won't be representative of the final developer experience once the New Architecture is stable. We're working on tools, templates and libraries to help you get started fast on the New Architecture, without having to go through the whole setup.
你已将原生模块的 JavaScript 规范定义为 prerequisites 的一部分,现在你已准备好将库迁移到新架构。 你可以按照以下步骤来完成此操作。
英You have defined the JavaScript specs for your native modules as part of the prerequisites, and you are now ready to migrate your library to the New Architecture. Here are the steps you can follow to accomplish this.
1. 更新 Podspec 以适应新架构
新架构利用了 CocoaPods。
英The New Architecture makes use of CocoaPods.
添加愚蠢和其他依赖
新架构需要一些特定的依赖才能正常工作。 你可以通过修改 .podspec
文件将 podspec 设置为自动安装所需的依赖。 在 Pod::Spec.new
块中,添加以下行:
英The New Architecture requires some specific dependencies to work properly. You can set up your podspec to automatically install the required dependencies by modifying the .podspec
file. In your Pod::Spec.new
block, add the following line:
Pod::Spec.new do |s|
# ...
+ install_modules_dependencies(s)
# ...
end
在这个 link,你可以找到 install_modules_dependencies
函数的文档。
英At this link, you can find the documentation of the install_modules_dependencies
function.
如果你需要明确知道正在使用哪个 folly_flags
React Native,可以使用 folly_flag
函数查询它们。
英If you need to explicitly know which folly_flags
React Native is using, you can query them using the folly_flag
function.
2. 扩展或实现代码生成的原生接口
原生模块或组件的 JavaScript 规范将用于为每个受支持的平台(即 Android 和 iOS)生成原生接口代码。 当构建依赖于你的库的 React Native 应用时,将生成这些原生接口文件。
英The JavaScript spec for your native module or component will be used to generate native interface code for each supported platform (i.e., Android and iOS). These native interface files will be generated when a React Native application that depends on your library is built.
虽然这生成了原生接口代码 不会作为你的库的一部分发货,但你确实需要确保你的 Objective-C 或 Java 代码符合这些原生接口文件提供的协议。 你可以使用 Codegen 脚本生成库的原生接口代码,以便使用它 作为参考。
英While this generated native interface code will not ship as part of your library, you do need to make sure your Objective-C or Java code conforms to the protocols provided by these native interface files. You can use the Codegen script to generate your library’s native interface code in order to use it as a reference.
cd <path/to/your/app>
node node_modules/react-native/scripts/generate-codegen-artifacts.js \
--path <your app>/ \
--outputPath <an/output/path> \
此命令将在作为参数提供的输出路径中生成 iOS 所需的样板代码。
英This command will generate the boilerplate code required by iOS in the output path provided as a parameter.
由脚本 不应该犯 输出的文件,但你需要引用它们来确定需要对原生模块进行哪些更改,以便它们为每个生成的 @protocol
/原生接口提供实现。
英The files that are output by the script should not be committed, but you’ll need to refer to them to determine what changes you need to make to your native modules in order for them to provide an implementation for each generated @protocol
/ native interface.
符合原生接口代码提供的协议
更新你的原生模块或组件,以确保它实现/扩展根据 JavaScript 规范生成的原生接口。
英Update your native module or component to ensure it implements/extends the native interface that has been generated from your JavaScript specs.
按照上一节中阐述的示例,你的库可能会导入 MyAwesomeSpecs.h
,扩展相关的原生接口,并实现该接口所需的方法:
英Following the example set forth in the previous section, your library might import MyAwesomeSpecs.h
, extend the relevant native interface, and implement the necessary methods for this interface:
#import <MyAwesomeSpecs/MyAwesomeSpecs.h>
@interface MyAwesomeModule () <StringGetterSpec>
@end
RCT_EXPORT_METHOD(getString:(NSString *)string
callback:(RCTResponseSenderBlock)callback)
{
// Implement this method
}
- (std::shared_ptr<TurboModule>)getTurboModule:(const ObjCTurboModule::InitParams &)params
{
return std::make_shared<StringGetterSpecJSI>(params);
}
对于现有的原生模块,你可能已经拥有一个或多个 RCT_EXPORT_METHOD
实例。 要迁移到新架构,你需要确保方法签名使用 Codegen 输出提供的结构。
英For an existing native module, you will likely already have one or more instances of RCT_EXPORT_METHOD
. To migrate to the New Architecture, you’ll need to make sure the method signature uses the structs provided by the Codegen output.