How do I support consuming SPM packages in CocoaPods dependencies?

Asked on 08/02/2024

1 search

To support consuming Swift Package Manager (SPM) packages in CocoaPods dependencies, you can follow these general steps:

  1. Create a Swift Package: First, you need to create a Swift package that includes the dependencies you want to use. You can do this using the Swift Package Manager from the command line or within Xcode.

  2. Add Dependencies: In your Swift package, add the dependencies you need by specifying them in the Package.swift file. For example:

    dependencies: [
        .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
    ],
    
  3. Build the Package: Use the Swift Package Manager to build and test your package to ensure everything is working correctly. You can do this from the command line:

    swift build
    swift test
    
  4. Integrate with CocoaPods: To use the Swift package in a CocoaPods project, you can create a podspec file for your Swift package. This file describes the package and its dependencies. Here is an example of a simple podspec file:

    Pod::Spec.new do |s|
      s.name         = 'MySwiftPackage'
      s.version      = '1.0.0'
      s.summary      = 'A short description of MySwiftPackage.'
      s.description  = <<-DESC
                       A longer description of MySwiftPackage.
                       DESC
      s.homepage     = 'https://example.com/MySwiftPackage'
      s.license      = { :type => 'MIT', :file => 'LICENSE' }
      s.author       = { 'Author' => 'author@example.com' }
      s.source       = { :git => 'https://github.com/username/MySwiftPackage.git', :tag => s.version.to_s }
      s.source_files  = 'Sources/**/*.swift'
      s.swift_version = '5.0'
      s.dependency 'SwiftPackageDependency', '~> 1.0'
    end
    
  5. Publish the Pod: Once your podspec file is ready, you can publish your pod to the CocoaPods repository. This will make it available for other projects to use.

For more detailed guidance, you can refer to the Swift Package Manager documentation and the CocoaPods guides.

Relevant Sessions from WWDC

These sessions provide insights into Swift Package Manager, Swift 6, and other related topics that might be useful for your integration.