capacitor-plugin-spm-support
Original:🇺🇸 English
Translated
Guides the agent through adding Swift Package Manager (SPM) support to an existing Capacitor plugin. Covers creating a Package.swift manifest, replacing Objective-C bridge files with the CAPBridgedPlugin Swift protocol, updating .gitignore for SPM artifacts, cleaning up the Xcode project file, and updating package.json. Do not use for Capacitor app projects, creating new plugins from scratch, or non-Capacitor plugin frameworks.
5installs
Sourcecapawesome-team/skills
Added on
NPX Install
npx skill4agent add capawesome-team/skills capacitor-plugin-spm-supportTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Add SPM Support to a Capacitor Plugin
Add Swift Package Manager (SPM) support to an existing Capacitor plugin by replacing the Objective-C bridge with the Swift protocol and adding a manifest.
CAPBridgedPluginPackage.swiftPrerequisites
| Requirement | Version |
|---|---|
| Capacitor | 6+ |
| Swift | 5.9+ |
| Xcode | 15+ |
The project must be a Capacitor plugin (not an app project). The plugin must have an existing iOS implementation with Swift source files in .
ios/Plugin/Procedures
Step 1: Gather Plugin Information
- Read in the plugin root. Extract:
package.json- The plugin package name (e.g., ).
@capawesome/capacitor-app-review - The existing array entries.
files - The existing entries.
scripts
- The plugin package name (e.g.,
- Read the file in the plugin root. Extract:
.podspec- The pod name (the argument, e.g.,
Pod::Spec.new). This becomes the SPM package name.CapawesomeCapacitorAppReview - The iOS deployment target from (e.g.,
s.ios.deployment_target). Extract the major version number (e.g.,'13.0'). This becomes the SPM iOS version.13 - All third-party CocoaPods dependencies — any or
s.dependencyentries that are notspec.dependencyorCapacitor. Record each dependency name and version constraint.CapacitorCordova
- The pod name (the
- Identify the plugin Swift file in . It contains a class extending
ios/Plugin/withCAPPlugin. Extract:@objc(<PluginClassName>)- The plugin class name (e.g., ).
AppReviewPlugin - The JavaScript name from the Objective-C file's
.mmacro first string argument (e.g.,CAP_PLUGIN).AppReview - All plugin methods from macro calls in the
CAP_PLUGIN_METHODfile, noting each method's name and return type (e.g.,.m).CAPPluginReturnPromise
- The plugin class name (e.g.,
- Identify the Objective-C bridge files in :
ios/Plugin/- (header file)
<PluginClassName>.h - (implementation file with
<PluginClassName>.mmacro)CAP_PLUGIN
- Read the Capacitor peer dependency version from (
package.json). Determine the major version (e.g.,peerDependencies["@capacitor/core"]). This is the Capacitor major version.6
Step 2: Resolve CocoaPods Dependencies for SPM
Skip this step if no third-party CocoaPods dependencies were found in Step 1.
For each third-party CocoaPods dependency, an equivalent SPM-compatible package is needed. Present the list of dependencies to the user and ask whether they can provide the SPM package URLs themselves, or whether the agent should search the web for SPM equivalents.
If the user provides SPM package URLs: Record them and proceed to Step 3.
If the user requests a web search: For each CocoaPods dependency:
- Search the web for to determine whether the original CocoaPods dependency also supports SPM. Many popular libraries (e.g., Firebase, Alamofire) distribute via both CocoaPods and SPM from the same repository.
"<dependency_name>" Swift Package Manager - If the original library supports SPM, use its Git repository URL. Use the same version as specified in the podspec — convert the CocoaPods version constraint to the SPM equivalent (e.g., becomes
~> 5.0,.upToNextMajor(from: "5.0.0")becomes= 2.1.0)..exact("2.1.0") - If the original library does not support SPM, search for to find a replacement package that provides equivalent functionality via SPM. Use a version that is compatible with the version used in the podspec.
"<dependency_name>" SPM alternative - If no SPM-compatible alternative exists, inform the user and ask how to proceed.
Record the resolved SPM package URL, version requirement, and product name(s) for each dependency. These will be added to in the next step.
Package.swiftStep 3: Create Package.swift
Package.swiftCreate in the plugin root directory with the following content:
Package.swiftswift
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "<SPM_PACKAGE_NAME>",
platforms: [.iOS(.v<SPM_IOS_VERSION>)],
products: [
.library(
name: "<SPM_PACKAGE_NAME>",
targets: ["<PLUGIN_CLASS_NAME>"])
],
dependencies: [
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", branch: "<CAPACITOR_MAJOR_VERSION>.0.0")
// <ADDITIONAL_PACKAGE_DEPENDENCIES>
],
targets: [
.target(
name: "<PLUGIN_CLASS_NAME>",
dependencies: [
.product(name: "Capacitor", package: "capacitor-swift-pm"),
.product(name: "Cordova", package: "capacitor-swift-pm")
// <ADDITIONAL_TARGET_DEPENDENCIES>
],
path: "ios/Plugin"),
.testTarget(
name: "<PLUGIN_CLASS_NAME>Tests",
dependencies: ["<PLUGIN_CLASS_NAME>"],
path: "ios/PluginTests")
]
)Replace all placeholders:
- — the SPM iOS version from Step 1 (e.g.,
<SPM_IOS_VERSION>).13 - — the pod name from Step 1 (e.g.,
<SPM_PACKAGE_NAME>).CapawesomeCapacitorAppReview - — the plugin class name from Step 1 (e.g.,
<PLUGIN_CLASS_NAME>).AppReviewPlugin - — the Capacitor major version from Step 1 (e.g.,
<CAPACITOR_MAJOR_VERSION>).6 - — if third-party dependencies were resolved in Step 2, add a
<ADDITIONAL_PACKAGE_DEPENDENCIES>entry for each. Remove the comment line if no extra dependencies exist..package(url: "<repo_url>", <version_requirement>) - — for each package dependency added above, add a corresponding
<ADDITIONAL_TARGET_DEPENDENCIES>entry. Remove the comment line if no extra dependencies exist..product(name: "<ProductName>", package: "<package-name>")
Step 4: Update the Swift Plugin Class
Open the plugin Swift file (e.g., ).
ios/Plugin/<PluginClassName>.swift- Add protocol conformance to the class declaration.
CAPBridgedPlugin - Add the three required properties as the first properties in the class body, before any existing properties.
Apply this diff pattern:
diff
@objc(<PluginClassName>)
-public class <PluginClassName>: CAPPlugin {
+public class <PluginClassName>: CAPPlugin, CAPBridgedPlugin {
+ public let identifier = "<PluginClassName>"
+ public let jsName = "<JS_NAME>"
+ public let pluginMethods: [CAPPluginMethod] = [
+ CAPPluginMethod(name: "<method1>", returnType: CAPPluginReturnPromise),
+ CAPPluginMethod(name: "<method2>", returnType: CAPPluginReturnPromise)
+ ]Replace:
- — the plugin class name (e.g.,
<PluginClassName>).AppReviewPlugin - — the JavaScript name from the
<JS_NAME>file's.mmacro (e.g.,CAP_PLUGIN).AppReview - The array — list all methods from the
pluginMethodsfile's.mcalls, preserving each method's name and return type exactly.CAP_PLUGIN_METHOD
Step 5: Delete Objective-C Bridge Files
Delete the following files from :
ios/Plugin/<PluginClassName>.h<PluginClassName>.m
These are no longer needed because the plugin registration is now handled by the protocol in Swift.
CAPBridgedPluginStep 6: Clean Up the Xcode Project File
Open and remove all references to the deleted Objective-C files. Specifically, remove lines referencing:
ios/Plugin.xcodeproj/project.pbxproj- — file references, build phase entries (
<PluginClassName>.h,PBXBuildFile,PBXFileReferencechildren,PBXGroup)PBXHeadersBuildPhase - — file references, build phase entries (
<PluginClassName>.m,PBXBuildFile,PBXFileReferencechildren,PBXGroup)PBXSourcesBuildPhase
Search for both filenames in the file and remove every line that references them.
.pbxprojStep 7: Update .gitignore
.gitignoreOpen in the plugin root. Add the following entries if not already present:
.gitignorediff
# iOS files
+Package.resolved
+/.build
+/Packages
+.swiftpm/configuration/registries.json
+.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
+.netrcPlace these entries in the iOS section of the file, after any existing iOS-related entries (e.g., , ).
.gitignorePodsPodfile.lockStep 8: Update package.json
package.jsonApply two changes to :
package.json- Add to the
"Package.swift"array:files
diff
"ios/Plugin/",
- "<PodName>.podspec"
+ "<PodName>.podspec",
+ "Package.swift"
],- Add the script to the
ios:spm:installobject:scripts
diff
"ios:pod:install": "cd ios && pod install --repo-update && cd ..",
+ "ios:spm:install": "cd ios && swift package resolve && cd ..",If the script does not exist, add the script after the last existing script entry.
ios:pod:installios:spm:installStep 9: Verify
- Run in the plugin root to ensure
npm installis valid.package.json - Verify the iOS build still succeeds by building the plugin's example or test app.
Error Handling
- If the file becomes corrupted after removing ObjC references, restore it from version control and carefully re-edit, ensuring only complete lines are removed.
.pbxproj - If the Swift build fails with not found, verify that
CAPBridgedPluginis version 6+ and that@capacitor/corebranch matches the Capacitor major version.capacitor-swift-pm - If SPM resolution fails (), verify the
swift package resolvetarget paths match the actual directory structure (Package.swiftfor sources,ios/Pluginfor tests).ios/PluginTests - If the plugin has no test target directory (), remove the
ios/PluginTestsblock from.testTarget.Package.swift - If the plugin has additional Swift source files beyond the main plugin file, no extra changes are needed — SPM automatically includes all files in the target path.
.swift - If the file uses
.minstead ofCAPPluginReturnNonefor some methods, preserve the original return type in theCAPPluginReturnPromisearray.pluginMethods - If a CocoaPods dependency has no SPM equivalent and no alternative can be found, the plugin cannot fully support SPM. Inform the user and suggest they either vendor the dependency source or wait for upstream SPM support.