重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
cocoapods-to-spm by cap-go/capgo-skills
npx skills add https://github.com/cap-go/capgo-skills --skill cocoapods-to-spm将 Capacitor iOS 项目从 CocoaPods 迁移到 Swift Package Manager 的逐步指南。
| 方面 | CocoaPods | SPM |
|---|---|---|
| 构建速度 | 较慢 | 更快 |
| Apple 集成 | 第三方 | 原生 Xcode |
| Ruby 依赖 | 必需 | 无 |
| 版本管理 | Podfile.lock | Package.resolved |
| Xcodeproj 变更 | 修改项目 | 使用工作空间 |
| 二进制缓存 | 有限 | 内置 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
首先,确定您当前正在使用的内容:
cd ios/App
cat Podfile
pod outdated
需要迁移的常见 Capacitor pods:
# Podfile (迁移前)
target 'App' do
capacitor_pods
pod 'Firebase/Analytics'
pod 'Firebase/Messaging'
pod 'Alamofire'
pod 'KeychainAccess'
end
大多数流行的库都支持 SPM。使用以下 URL:
| 库 | SPM URL |
|---|---|
| Firebase | https://github.com/firebase/firebase-ios-sdk |
| Alamofire | https://github.com/Alamofire/Alamofire |
| KeychainAccess | https://github.com/kishikawakatsumi/KeychainAccess |
| SDWebImage | https://github.com/SDWebImage/SDWebImage |
| SnapKit | https://github.com/SnapKit/SnapKit |
| Realm | https://github.com/realm/realm-swift |
| Lottie | https://github.com/airbnb/lottie-spm |
cd ios/App
# 移除 CocoaPods 集成
pod deintegrate
# 移除 Podfile.lock 和 Pods 目录
rm -rf Podfile.lock Pods
# 移除工作空间(我们将直接使用项目或创建新的工作空间)
rm -rf App.xcworkspace
ios/App/App.xcodeprojAppCapacitor 核心仍然需要 CocoaPods。创建最小化的 Podfile:
# ios/App/Podfile
require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'
platform :ios, '14.0'
use_frameworks!
install! 'cocoapods', :disable_input_output_paths => true
def capacitor_pods
pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
end
target 'App' do
capacitor_pods
# 其他尚不支持 SPM 的插件 pods
end
post_install do |installer|
assertDeploymentTarget(installer)
end
然后运行:
cd ios/App && pod install
如果您正在创建一个支持 SPM 的 Capacitor 插件:
Package.swift :
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "CapacitorNativeBiometric",
platforms: [.iOS(.v14)],
products: [
.library(
name: "CapacitorNativeBiometric",
targets: ["NativeBiometricPlugin"]
),
],
dependencies: [
.package(url: "https://github.com/nicholasalx/capacitor-swift-pm", from: "6.0.0"),
],
targets: [
.target(
name: "NativeBiometricPlugin",
dependencies: [
.product(name: "Capacitor", package: "capacitor-swift-pm"),
.product(name: "Cordova", package: "capacitor-swift-pm"),
],
path: "ios/Sources/NativeBiometricPlugin",
publicHeadersPath: "include"
),
]
)
ios/
├── App/
│ ├── App/
│ │ ├── AppDelegate.swift
│ │ ├── Info.plist
│ │ └── ...
│ ├── App.xcodeproj/
│ │ └── project.xcworkspace/
│ │ └── xcshareddata/
│ │ └── swiftpm/
│ │ └── Package.resolved # SPM 锁定文件
│ ├── Podfile
│ └── Podfile.lock
└── ...
大多数 Capacitor 项目最适合采用混合方法:
@capacitor/ios 核心Podfile :
require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'
platform :ios, '14.0'
use_frameworks!
install! 'cocoapods', :disable_input_output_paths => true
def capacitor_pods
pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
# 不支持 SPM 的插件
pod 'CapacitorCamera', :path => '../../node_modules/@capacitor/camera'
end
target 'App' do
capacitor_pods
# 此处不要放 Firebase - 改用 SPM
end
Xcode 包依赖项:
原因:同一个库同时存在于 CocoaPods 和 SPM 中
解决方案:如果使用 SPM,则从 Podfile 中移除
# Podfile - 错误示例
pod 'Firebase/Analytics' # 移除这一行
# 改为在 Xcode 中使用 SPM
原因:SPM 包未链接到目标
解决方案:
原因:缺少框架或导入错误
解决方案:清理并重新构建
# 清理派生数据
rm -rf ~/Library/Developer/Xcode/DerivedData
# 在 Xcode 中清理构建文件夹
# Cmd + Shift + K
# 重新构建
bunx cap sync ios
cd ios/App && pod install
原因:插件需要注册
解决方案:确保插件在 AppDelegate.swift 中注册:
import Capacitor
import YourPlugin
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Capacitor 会自动处理插件注册
return true
}
}
my-capacitor-plugin/
├── Package.swift
├── ios/
│ └── Sources/
│ └── MyPlugin/
│ ├── MyPlugin.swift
│ ├── MyPlugin.m # 如果需要 Objective-C 桥接
│ └── include/
│ └── MyPlugin.h # 公共头文件
├── src/
│ ├── index.ts
│ ├── definitions.ts
│ └── web.ts
└── package.json
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "CapacitorMyPlugin",
platforms: [.iOS(.v14)],
products: [
.library(
name: "CapacitorMyPlugin",
targets: ["MyPluginPlugin"]
),
],
dependencies: [
.package(url: "https://github.com/nicholasalx/capacitor-swift-pm", from: "6.0.0"),
],
targets: [
.target(
name: "MyPluginPlugin",
dependencies: [
.product(name: "Capacitor", package: "capacitor-swift-pm"),
.product(name: "Cordova", package: "capacitor-swift-pm"),
],
path: "ios/Sources/MyPlugin"
),
]
)
import Foundation
import Capacitor
@objc(MyPlugin)
public class MyPlugin: CAPPlugin, CAPBridgedPlugin {
public let identifier = "MyPlugin"
public let jsName = "MyPlugin"
public let pluginMethods: [CAPPluginMethod] = [
CAPPluginMethod(name: "echo", returnType: CAPPluginReturnPromise),
]
@objc func echo(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
call.resolve(["value": value])
}
}
pod deintegratepod installPackage.resolved 提交到 git每周安装次数
52
代码仓库
GitHub 星标数
18
首次出现
2026年2月6日
安全审计
已安装于
gemini-cli50
opencode48
github-copilot46
codex46
kimi-cli44
amp43
Step-by-step guide for migrating Capacitor iOS projects from CocoaPods to Swift Package Manager.
| Aspect | CocoaPods | SPM |
|---|---|---|
| Build Speed | Slower | Faster |
| Apple Integration | Third-party | Native Xcode |
| Ruby Dependency | Required | None |
| Version Management | Podfile.lock | Package.resolved |
| Xcodeproj Changes | Modifies project | Uses workspace |
| Binary Caching | Limited | Built-in |
First, identify what you're currently using:
cd ios/App
cat Podfile
pod outdated
Common Capacitor pods to migrate:
# Podfile (before)
target 'App' do
capacitor_pods
pod 'Firebase/Analytics'
pod 'Firebase/Messaging'
pod 'Alamofire'
pod 'KeychainAccess'
end
Most popular libraries support SPM. Use these URLs:
| Library | SPM URL |
|---|---|
| Firebase | https://github.com/firebase/firebase-ios-sdk |
| Alamofire | https://github.com/Alamofire/Alamofire |
| KeychainAccess | https://github.com/kishikawakatsumi/KeychainAccess |
| SDWebImage | https://github.com/SDWebImage/SDWebImage |
| SnapKit | https://github.com/SnapKit/SnapKit |
| Realm | https://github.com/realm/realm-swift |
cd ios/App
# Remove CocoaPods integration
pod deintegrate
# Remove Podfile.lock and Pods directory
rm -rf Podfile.lock Pods
# Remove workspace (we'll use project directly or create new workspace)
rm -rf App.xcworkspace
ios/App/App.xcodeproj in XcodeAppCapacitor still needs CocoaPods for its core. Create minimal Podfile:
# ios/App/Podfile
require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'
platform :ios, '14.0'
use_frameworks!
install! 'cocoapods', :disable_input_output_paths => true
def capacitor_pods
pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
end
target 'App' do
capacitor_pods
# Other plugin pods that don't support SPM yet
end
post_install do |installer|
assertDeploymentTarget(installer)
end
Then run:
cd ios/App && pod install
If you're creating a Capacitor plugin with SPM support:
Package.swift :
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "CapacitorNativeBiometric",
platforms: [.iOS(.v14)],
products: [
.library(
name: "CapacitorNativeBiometric",
targets: ["NativeBiometricPlugin"]
),
],
dependencies: [
.package(url: "https://github.com/nicholasalx/capacitor-swift-pm", from: "6.0.0"),
],
targets: [
.target(
name: "NativeBiometricPlugin",
dependencies: [
.product(name: "Capacitor", package: "capacitor-swift-pm"),
.product(name: "Cordova", package: "capacitor-swift-pm"),
],
path: "ios/Sources/NativeBiometricPlugin",
publicHeadersPath: "include"
),
]
)
ios/
├── App/
│ ├── App/
│ │ ├── AppDelegate.swift
│ │ ├── Info.plist
│ │ └── ...
│ ├── App.xcodeproj/
│ │ └── project.xcworkspace/
│ │ └── xcshareddata/
│ │ └── swiftpm/
│ │ └── Package.resolved # SPM lock file
│ ├── Podfile
│ └── Podfile.lock
└── ...
Most Capacitor projects work best with a hybrid approach:
@capacitor/ios corePodfile :
require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'
platform :ios, '14.0'
use_frameworks!
install! 'cocoapods', :disable_input_output_paths => true
def capacitor_pods
pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
# Plugins without SPM support
pod 'CapacitorCamera', :path => '../../node_modules/@capacitor/camera'
end
target 'App' do
capacitor_pods
# NO Firebase here - use SPM instead
end
Xcode Package Dependencies :
Cause : Same library in both CocoaPods and SPM
Solution : Remove from Podfile if using SPM
# Podfile - WRONG
pod 'Firebase/Analytics' # Remove this
# Use SPM instead in Xcode
Cause : SPM package not linked to target
Solution :
Cause : Missing frameworks or wrong imports
Solution : Clean and rebuild
# Clean derived data
rm -rf ~/Library/Developer/Xcode/DerivedData
# Clean build folder in Xcode
# Cmd + Shift + K
# Rebuild
bunx cap sync ios
cd ios/App && pod install
Cause : Plugin needs registration
Solution : Ensure plugin is registered in AppDelegate.swift:
import Capacitor
import YourPlugin
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Capacitor handles plugin registration automatically
return true
}
}
my-capacitor-plugin/
├── Package.swift
├── ios/
│ └── Sources/
│ └── MyPlugin/
│ ├── MyPlugin.swift
│ ├── MyPlugin.m # Objc bridge if needed
│ └── include/
│ └── MyPlugin.h # Public headers
├── src/
│ ├── index.ts
│ ├── definitions.ts
│ └── web.ts
└── package.json
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "CapacitorMyPlugin",
platforms: [.iOS(.v14)],
products: [
.library(
name: "CapacitorMyPlugin",
targets: ["MyPluginPlugin"]
),
],
dependencies: [
.package(url: "https://github.com/nicholasalx/capacitor-swift-pm", from: "6.0.0"),
],
targets: [
.target(
name: "MyPluginPlugin",
dependencies: [
.product(name: "Capacitor", package: "capacitor-swift-pm"),
.product(name: "Cordova", package: "capacitor-swift-pm"),
],
path: "ios/Sources/MyPlugin"
),
]
)
import Foundation
import Capacitor
@objc(MyPlugin)
public class MyPlugin: CAPPlugin, CAPBridgedPlugin {
public let identifier = "MyPlugin"
public let jsName = "MyPlugin"
public let pluginMethods: [CAPPluginMethod] = [
CAPPluginMethod(name: "echo", returnType: CAPPluginReturnPromise),
]
@objc func echo(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
call.resolve(["value": value])
}
}
pod deintegratepod installPackage.resolved to gitWeekly Installs
52
Repository
GitHub Stars
18
First Seen
Feb 6, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli50
opencode48
github-copilot46
codex46
kimi-cli44
amp43
yfinance MCP 服务器 - 获取 Yahoo Finance 实时和历史金融数据 | 股票价格、期权、财报、新闻
75 周安装
Docker专家技能:NestJS/Next.js容器化、Docker Compose配置与微服务编排最佳实践
74 周安装
Python数据分析技能 - 掌握Pandas、NumPy数据操作与Jupyter工作流
73 周安装
市场调研技能:全面指南与框架,助您分析竞争对手、识别市场机会
76 周安装
AI创意写作助手 - 生成小说、诗歌、剧本、微小说,支持多种体裁风格
74 周安装
前端设计最佳实践:从排版、色彩到动效与空间构成的专业指南
77 周安装
| Lottie | https://github.com/airbnb/lottie-spm |