Skip to content

feat(ios): add Swift Package Manager support - #1232

Merged
Naturalclar merged 6 commits into
react-native-menu:masterfrom
chrfalch:spm-support
Nov 18, 2026
Merged

Naturalclar merged 6 commits into
react-native-menu:masterfrom
chrfalch:spm-support

Conversation

@chrfalch

@chrfalch chrfalch commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Overview

React Native 0.88 can build apps with Swift Package Manager. React Native 0.89 plans to make it the default. This library has no Package.swift, so SwiftPM apps cannot use it.

  1. Add Package.swift with separate Swift and Objective-C++ targets.
  2. Add swiftpmConfig.name and ship the package support files through npm.
  3. Add a hand-written Swift API header and a compile-time Swift API pin.

Under SwiftPM, the Objective-C++ code cannot import Xcode's generated Swift header. spm-headers/react_native_menu-Swift.h declares the needed Swift API by hand. spm-support/SwiftHeaderPin.swift makes the SwiftPM build fail if the checked Swift API changes. Both files must be updated together.
MenuView.h stays private and is not exposed to other packages.

CocoaPods is unchanged. The podspec and ios/ sources are untouched.

Limits: iOS 15.1 and later. The pin cannot check Objective-C class or member name strings. No CI job here builds the Swift package yet.
Closes #1231.
RFC: react-native-community/discussions-and-proposals#994

Test Plan

How I tested

In a new React Native 0.88.1 app, with the library installed from the local checkout:

npx react-native spm add --deintegrate --yes
npx react-native spm update
xcodebuild -project ios/<App>.xcodeproj -scheme <App> -configuration Debug -destination "generic/platform=iOS Simulator" CODE_SIGNING_ALLOWED=NO build
xcodebuild -project ios/<App>.xcodeproj -scheme <App> -configuration Release -destination "generic/platform=iOS Simulator" CODE_SIGNING_ALLOWED=NO build

Add and update passed. Debug and Release ended with ** BUILD SUCCEEDED **.
Changing onOpenMenu to return Bool in the protocol and both implementations built with the pin removed, then failed in the restored pin. Restoring the API built successfully.
Runtime check: the simulator menu opened with two actions. Selecting First action returned first and showed selected action: first.

@chrfalch
chrfalch requested a review from Naturalclar as a code owner November 3, 2026 16:26

@Naturalclar Naturalclar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@Naturalclar
Naturalclar merged commit cd2148a into react-native-menu:master Nov 18, 2026
5 checks passed
Naturalclar added a commit that referenced this pull request Nov 20, 2026
No build in this repository compiles the SwiftPM package: CI validates
native code through the CocoaPods example app, and `Package.swift` can
only resolve its two local dependencies inside React Native's SwiftPM
autolinking layout, which the pinned React Native does not generate. So
`spm-support/SwiftHeaderPin.swift` -- written to catch drift in the
hand-mirrored `spm-headers/react_native_menu-Swift.h` -- is never
compiled by anything, and in any case cannot catch an *addition* to the
Swift API.

Add `scripts/check-spm.mjs` and a `spm` CI job running the checks that
are possible without that graph:

- every native source CocoaPods compiles is in a SwiftPM target too, and
  nothing untracked leaks into one (via `swift package describe`, which
  evaluates the manifest without resolving dependencies);
- the mirrored header still matches `FabricViewImplementationProtocol`
  member for member, including the required/optional split, the property
  attributes, the mangled `objc_runtime_name`, and class availability.

Each guard was verified by breaking the thing it guards: an unmirrored
protocol member, a wrong mangled name, and a dropped availability
attribute each fail the check, and the tree passes.

Also from the review of #1232:

- name the ObjC target's source directories instead of all of `ios/`. A
  stray `.h`/`.m`/`.mm` anywhere under `ios/` was compiled into the
  target; with a planted `ios/build/Stray.mm`, `swift package describe`
  confirms it before this change and not after.
- restore the `@available(iOS 14.0, *)` that `FabricMenuViewImplementation`
  carries in Swift, as `API_AVAILABLE(ios(14.0))`, and record why the
  mirrored interfaces flatten their superclasses.
- replace the manifest's three `try!`s. A trapping manifest reports a
  Swift fatal error rather than a SwiftPM diagnostic. This one is
  hygiene: a dangling symlink under `ios/` does not actually throw on
  Swift 6.0.2, so the trap was not reproducible.

Refs #1256

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Swift Package Manager (React Native 0.87+)

2 participants