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
{{ message }}
Repository navigation
feat(ios): add Swift Package Manager support - #1367
React Native 0.88 can link iOS libraries with Swift Package Manager (SwiftPM) instead of CocoaPods. An app in SwiftPM mode cannot use AsyncStorage today. The library has no Package.swift, and React Native cannot generate one because the library mixes Swift and Objective-C++ sources. The build stops with an error about mixed Swift and Objective-C sources.
This PR adds SwiftPM support. No source file under apple/ changes, and the CocoaPods setup is not touched. It needs one change outside the package, in the Kotlin build (item 4).
packages/async-storage/Package.swift: a hand-written manifest with two targets, because SwiftPM cannot compile Swift and Objective-C++ in one target.
AsyncStorage (Swift): apple/storage/. It depends on the SharedAsyncStorage binary target (apple-frameworks/SharedAsyncStorage.xcframework, as in the podspec). The Swift module name stays AsyncStorage, so the Objective-C runtime class names do not change.
AsyncStorageObjC (Objective-C++): the rest of apple/, including the legacy storage. It depends on the Swift target. It bundles PrivacyInfo.xcprivacy, like the podspec's resource bundle.
The product is AsyncStorage, the same name as the podspec, and contains both targets.
It defines RCT_NEW_ARCH_ENABLED=1 for C, Objective-C and C++, which CocoaPods gets from install_modules_dependencies.
It targets iOS 15.1 only. The podspec also lists macOS and visionOS; I left them out because I could not test them.
packages/async-storage/spm-headers/AsyncStorage-Swift.h: declarations of the two Swift classes (RNStorage, StorageRegistry) for AsyncStorage.mm. In a SwiftPM build, Xcode gives an Objective-C++ target no header search path to the Swift target's generated header, and C++ module imports are off. This file has the same declarations as the generated header. It must be updated when the @objc API in apple/storage/ changes. If you prefer a different approach, for example a small change in AsyncStorage.mm, I am happy to change it.
packages/async-storage/spm-support/Foundation.swift: one line, @_exported import Foundation. The Swift sources rely on Foundation being imported implicitly, as CocoaPods does.
shared-storage/build.gradle.kts: the framework's bundle ID changes from org.asyncstorage.shared_storage to org.asyncstorage.shared-storage. SwiftPM copies the framework into the app, and Xcode's app validation rejects the underscore: Framework ... SharedAsyncStorage.framework had an invalid CFBundleIdentifier in its Info.plist: org.asyncstorage.shared_storage. CocoaPods only links the framework, so it was not affected. SwiftPM support works only after a release that contains a framework rebuilt with this change.
packages/async-storage/package.json: adds Package.swift, spm-headers and spm-support to files, and sets swiftpmConfig.name, which React Native 0.88 reads for the SwiftPM product name.
.changeset/swiftpm-support.md: a minor changeset. Change the level if you prefer.
Test Plan
New React Native 0.88.0 app, switched to SwiftPM with npx react-native spm add --deintegrate, AsyncStorage from this branch. The xcframework was copied from the published 3.1.1 package, with its bundle ID changed by hand to match item 4.
Without this change: React Native reports mixed Swift and Objective-C/C++ sources and stops.
With this change: xcodebuild Debug for the iOS Simulator (arm64 and x86_64) ends with ** BUILD SUCCEEDED **. No warnings come from the manifest. PrivacyInfo.xcprivacy is in the built app.
With the unchanged 3.1.1 framework, the build fails on the bundle ID, as described in item 4.
Release build on an iPhone 18 Pro simulator (iOS 27.0): setItem and getItem round-trip a value, both through the default export and through createAsyncStorage.
Not tested: macOS, visionOS, a Gradle build of the framework, and installing from a published npm tarball. npm pack --dry-run runs prepare, which needs the full monorepo build, so I checked the files entries by reading them.
I added spm-support/SwiftHeaderPin.swift. Only the SwiftPM build compiles it. If the Swift API in apple/storage changes, this file stops compiling, so the hand-written SwiftPM header cannot get out of step with the Swift code without anyone seeing it.
Separately: the SharedAsyncStorage.xcframework in 3.1.1 has the bundle id org.asyncstorage.shared_storage. Xcode 27.2 rejects that id when it validates the app. SwiftPM apps need a release with the rebuilt framework, and the build.gradle.kts change in this PR makes that id valid.
I added spm-support/SwiftHeaderPin.swift. Only the SwiftPM build compiles it. If the Swift API in apple/storage changes, this file stops compiling, so the hand-written SwiftPM header cannot get out of step with the Swift code without anyone seeing it.
Separately: the SharedAsyncStorage.xcframework in 3.1.1 has the bundle id org.asyncstorage.shared_storage. Xcode 27.2 rejects that id when it validates the app. SwiftPM apps need a release with the rebuilt framework, and the build.gradle.kts change in this PR makes that id valid.
thanks @chrfalch for this - I will try to find time to review it this week 🙏
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
React Native 0.88 can link iOS libraries with Swift Package Manager (SwiftPM) instead of CocoaPods. An app in SwiftPM mode cannot use AsyncStorage today. The library has no
Package.swift, and React Native cannot generate one because the library mixes Swift and Objective-C++ sources. The build stops with an error about mixed Swift and Objective-C sources.This PR adds SwiftPM support. No source file under
apple/changes, and the CocoaPods setup is not touched. It needs one change outside the package, in the Kotlin build (item 4).packages/async-storage/Package.swift: a hand-written manifest with two targets, because SwiftPM cannot compile Swift and Objective-C++ in one target.AsyncStorage(Swift):apple/storage/. It depends on theSharedAsyncStoragebinary target (apple-frameworks/SharedAsyncStorage.xcframework, as in the podspec). The Swift module name staysAsyncStorage, so the Objective-C runtime class names do not change.AsyncStorageObjC(Objective-C++): the rest ofapple/, including the legacy storage. It depends on the Swift target. It bundlesPrivacyInfo.xcprivacy, like the podspec's resource bundle.AsyncStorage, the same name as the podspec, and contains both targets.RCT_NEW_ARCH_ENABLED=1for C, Objective-C and C++, which CocoaPods gets frominstall_modules_dependencies.packages/async-storage/spm-headers/AsyncStorage-Swift.h: declarations of the two Swift classes (RNStorage,StorageRegistry) forAsyncStorage.mm. In a SwiftPM build, Xcode gives an Objective-C++ target no header search path to the Swift target's generated header, and C++ module imports are off. This file has the same declarations as the generated header. It must be updated when the@objcAPI inapple/storage/changes. If you prefer a different approach, for example a small change inAsyncStorage.mm, I am happy to change it.packages/async-storage/spm-support/Foundation.swift: one line,@_exported import Foundation. The Swift sources rely on Foundation being imported implicitly, as CocoaPods does.shared-storage/build.gradle.kts: the framework's bundle ID changes fromorg.asyncstorage.shared_storagetoorg.asyncstorage.shared-storage. SwiftPM copies the framework into the app, and Xcode's app validation rejects the underscore:Framework ... SharedAsyncStorage.framework had an invalid CFBundleIdentifier in its Info.plist: org.asyncstorage.shared_storage. CocoaPods only links the framework, so it was not affected. SwiftPM support works only after a release that contains a framework rebuilt with this change.packages/async-storage/package.json: addsPackage.swift,spm-headersandspm-supporttofiles, and setsswiftpmConfig.name, which React Native 0.88 reads for the SwiftPM product name..changeset/swiftpm-support.md: a minor changeset. Change the level if you prefer.Test Plan
npx react-native spm add --deintegrate, AsyncStorage from this branch. The xcframework was copied from the published 3.1.1 package, with its bundle ID changed by hand to match item 4.xcodebuildDebug for the iOS Simulator (arm64 and x86_64) ends with** BUILD SUCCEEDED **. No warnings come from the manifest.PrivacyInfo.xcprivacyis in the built app.setItemandgetItemround-trip a value, both through the default export and throughcreateAsyncStorage.npm pack --dry-runrunsprepare, which needs the full monorepo build, so I checked thefilesentries by reading them.