Swift框架Podspec集成CommonCrypto失效,求适配多团队方案
Hey there, let's tackle this CommonCrypto integration headache for your Swift framework that needs to play nice with Cordova, React, and other tech stacks. I've worked through similar podspec quirks before, so here are actionable, tested solutions:
1. Ditch the Aggregate Target (Simplest & Most Reliable)
Aggregate targets can cause path conflicts and podspec recognition issues when sharing across teams. Instead, embed the CommonCrypto module map logic directly into your podspec. This way, pod install handles everything automatically for all consumers.
Add this to your framework's podspec:
Pod::Spec.new do |s| s.name = "YourSwiftFramework" s.version = "1.0.0" s.swift_version = "5.0+" s.platform = :ios, "12.0" # Adjust to your minimum supported version s.source_files = "YourSwiftFramework/**/*.{swift}" s.static_framework = true # Critical for Cordova/React Native compatibility # Configure Swift to find the CommonCrypto module s.pod_target_xcconfig = { "SWIFT_INCLUDE_PATHS" => "$(PODS_TARGET_SRCROOT)/CommonCrypto", "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/CommonCrypto" } # Auto-generate the module map during pod build s.script_phases = [ { :name => "Generate CommonCrypto Module Map", :execution_position => :before_compile, :script => <<-SCRIPT # Create directory for module map MODULE_DIR="${PODS_TARGET_SRCROOT}/CommonCrypto" mkdir -p "${MODULE_DIR}" # Write module map file cat > "${MODULE_DIR}/module.modulemap" <<EOF module CommonCrypto [system] { header "/usr/include/CommonCrypto/CommonCrypto.h" export * } EOF SCRIPT } ] end
Why this works:
- The script runs before compilation, ensuring the module map exists when Swift needs it
SWIFT_INCLUDE_PATHStells the Swift compiler exactly where to find the modulestatic_framework = trueensures compatibility with Cordova plugins and React Native native modules, which often expect static frameworks
2. If You Must Keep the Aggregate Target
If you need to retain the CommonCryptoModuleMap aggregate target for internal workflows, fix your podspec to properly reference it and adjust the script paths:
Step 1: Update the Aggregate Target Script
Modify your run script to use pod-friendly paths (avoid hardcoded directories that break during pod installation):
MODULE_MAP_DIR="${PODS_BUILD_DIR}/CommonCryptoModuleMap" if [ -d "${MODULE_MAP_DIR}" ]; then echo "${MODULE_MAP_DIR} already exists, skipping." exit 0 fi mkdir -p "${MODULE_MAP_DIR}" cat > "${MODULE_MAP_DIR}/module.modulemap" <<EOF module CommonCrypto [system] { header "/usr/include/CommonCrypto/CommonCrypto.h" export * } EOF
Step 2: Update Your PodSpec
Add a subspec for the module map and link it to your main framework:
Pod::Spec.new do |s| s.name = "YourSwiftFramework" s.version = "1.0.0" s.swift_version = "5.0+" s.platform = :ios, "12.0" s.source_files = "YourSwiftFramework/**/*.{swift}" s.static_framework = true # Subspec for CommonCrypto module map s.subspec "CommonCryptoSupport" do |ss| ss.name = "CommonCryptoSupport" ss.script_phases = [ { :name => "Setup CommonCrypto Module Map", :execution_position => :before_compile, :script => <<-SCRIPT MODULE_MAP_DIR="${PODS_BUILD_DIR}/CommonCryptoModuleMap" if [ -d "${MODULE_MAP_DIR}" ]; then echo "${MODULE_MAP_DIR} already exists, skipping." exit 0 fi mkdir -p "${MODULE_MAP_DIR}" cat > "${MODULE_MAP_DIR}/module.modulemap" <<EOF module CommonCrypto [system] { header "/usr/include/CommonCrypto/CommonCrypto.h" export * } EOF SCRIPT } ] ss.pod_target_xcconfig = { "SWIFT_INCLUDE_PATHS" => "${PODS_BUILD_DIR}/CommonCryptoModuleMap" } end # Make main framework depend on the subspec s.dependency "YourSwiftFramework/CommonCryptoSupport" end
3. Validate & Test
Once you've updated the podspec, run these checks to ensure everything works:
- Run
pod lib lintto catch podspec syntax or configuration errors - Create a test iOS project, add your framework as a pod dependency, and run
pod install - In a Swift file, add
import CommonCryptoand test a simple API call (e.g.,let digest = CC_MD5("test", CC_LONG("test".count), nil)) - Verify the framework works in a Cordova/React Native test project to confirm cross-team compatibility
内容的提问来源于stack exchange,提问作者Valentin Cousien

