额外的平台支持
了解如何添加对 macOS 和 tvOS 平台的支持。
Expo Modules API 为 Android 和 iOS 提供一流的支持。然而,由于所有 Apple 平台都基于相同的基础并使用相同的编程语言,因此针对 Expo 模块中的其他 树外平台 是可能的。
¥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.
目前,仅支持 macOS 和 tvOS 平台。本指南将引导你添加对这些平台的支持。
¥Currently, only macOS and tvOS platforms are supported. This guide will walk you through adding support for these platforms.
1
Use the "apple"
platform in expo-module.config.json
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 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:
{
- "platforms": ["ios"],
- "ios": {
- "modules": ["MyModule"]
- }
+ "platforms": ["apple"],
+ "apple": {
+ "modules": ["MyModule"]
+ }
}
2
Update the podspec to declare support for other platforms
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.
- s.platform = :ios, '13.4'
+ s.platforms = {
+ :ios => '13.4',
+ :tvos => '13.4',
+ :osx => '10.15'
+ }
Any changes in the podspec require running pod install
to have an effect.
3
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.
- For macOS: follow the official Install React Native for macOS guide from
react-native-macos
documentation. - 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 the Build Expo apps for TV guide.
4
Review the code for using APIs not supported on these platforms
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.
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.
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.