首页指南参考教程

额外的平台支持

了解如何添加对 macOS 和 tvOS 平台的支持。


Expo Modules API 为 Android 和 iOS 提供一流的支持。然而,由于所有 Apple 平台都基于相同的基础并使用相同的编程语言,因此针对 Expo 模块中的其他 树外平台 是可能的。目前仅支持 macOS 和 tvOS 平台。本指南将引导你添加对这些平台的支持。

¥Expo Modules API provides first-class support for Android and iOS. However, since all Apple platforms are based on the same foundation and use the same programming language, targeting other Out-of-Tree platforms in the Expo module is possible. At the moment, only macOS and tvOS platforms are supported. This guide will walk you through adding support for these platforms.

1

expo-module.config.json 中使用 "apple" 平台

¥Use the "apple" platform in expo-module.config.json

为了提供对其他 Apple 平台的无缝支持,Expo SDK 50 引入了通用 "apple" 平台来指示模块可以支持任何 Apple 平台的自动链接,并且是否在特定 CocoaPods 目标中链接模块的问题已移至 podspec 。如果你之前使用过 "ios",你可以安全地更换它:

¥To provide seamless support for other Apple platforms, Expo SDK 50 introduced a universal "apple" platform to instruct the autolinking that the module may support any of the Apple platform and the question whether to link the module in the specific CocoaPods target is moved off to the podspec. If you have used "ios" before, you can safely replace it:

expo-module.config.json
{
-   "platforms": ["ios"],
-   "ios": {
-     "modules": ["MyModule"]
-   }
+   "platforms": ["apple"],
+   "apple": {
+     "modules": ["MyModule"]
+   }
}

2

更新 podspec 以声明对其他平台的支持

¥Update the podspec to declare support for other platforms

该模块的 podspec 需要使用支持的平台列表进行更新。否则,CocoaPods 将无法在其他平台的目标上安装 pod。正如第一步中提到的,当模块配置了通用 "apple" 平台时,规范的这一部分是自动链接的真实来源。

¥The module's podspec needs to be updated with a list of the supported platforms. Otherwise, CocoaPods would fail to install the pod on targets for the other platforms. As mentioned in the first step, this part of the spec is the source of truth for autolinking when the module is configured with a universal "apple" platform.

Module's podspec
- s.platform       = :ios, '13.4'
+ s.platforms = {
+   :ios => '13.4',
+   :tvos => '13.4',
+   :osx => '10.15'
+ }

podspec 中的任何更改都需要运行 pod install 才能生效。

¥Any changes in the podspec require running pod install to have an effect.

3

在应用中设置 react-native-macosreact-native-tvos

¥Set up react-native-macos or react-native-tvos in the app

如果你正在编写本地模块并且你的应用已设置,则可以跳过此步骤。否则,如果你正在编写独立(非本地)模块,则需要设置你的应用或示例应用。

¥If you are writing a local module and your app is already set up, you can skip this step. Otherwise, you will need to set up your app or the example app if you are writing a standalone (non-local) module.

  • 对于 macOS:请遵循 react-native-macos 文档中的官方 安装适用于 macOS 的 React Native 指南。

    ¥For macOS: follow the official Install React Native for macOS guide from react-native-macos documentation.

  • 对于电视操作系统:按照 react-native-tvos 存储库中的说明进行操作。如果你正在构建 Expo 应用,你还应该遵循我们的 用于电视指南的 Building Expo 应用 中的说明。

    ¥For tvOS: follow the instructions in the react-native-tvos repository. If you are building an Expo app, you should also follow the instructions in our Building Expo apps for TV guide.

4

查看使用这些平台不支持的 API 的代码

¥Review the code for using APIs not supported on these platforms

Apple 平台之间的平台 API 可能有所不同。最明显的区别来自于依赖不同的 UI 框架 - iOS/tvOS 上的 UIKit 和 macOS 上的 AppKit

¥Platform APIs may differ between Apple platforms. The most noticeable difference comes from relying on different UI frameworks —UIKit on iOS/tvOS and AppKit on macOS.

react-native-macosexpo-modules-core 都提供别名和填充来引用 macOS 目标上的 UIKit 类(例如,UIViewNSView 的别名,UIApplicationNSApplication 的别名),但对于 iOS 优先的库来说,通常不足以支持其他平台。 盒子。你可能需要编写根据平台使用不同实现的条件编译代码。

¥Both react-native-macos and expo-modules-core provide aliases and polyfills to referenceUIKit classes on macOS target (for example, UIView is an alias to NSView, UIApplication is an alias to NSApplication), but it's usually not enough for iOS-first libraries to support other platforms out of the box. You may need to write conditionally compiled code that uses different implementations depending on the platform.

为此,请使用带有 os 条件的 Swift 编译器指令,其中包括为特定平台构建应用时的给定代码段。与 #if#else 指令结合使用,你可以在跨平台代码中设置特定于平台的分支。

¥To do this, use Swift compiler directives with the os condition, which includes a given piece of code when our app is being built for a specific platform. In combination with the #if and #else directives, lets you set up platform-specific branches within the cross-platform code.

#if os(iOS)
  // iOS implementation
#elseif os(macOS)
  // macOS implementation
#elseif os(tvOS)
  // tvOS implementation
#endif

你的模块现在可以在 Out-of-Tree 平台上使用。

¥Your module is now ready to be used on Out-of-Tree platform.

Expo 中文网 - 粤ICP备13048890号