Publishing XCFrameworks with CocoaPods

Helder Pinhal
Helder Pinhal
Apr 23 2021
Posted in Engineering & Technology

Building and distributing XCFrameworks with CocoaPods

Publishing XCFrameworks with CocoaPods

In a previous post we went over the process of building a XCFramework and publishing it via the Swift Package Manager.

In this article we'll cover how we can do the same via CocoaPods. Although it's losing traction, CocoaPods still remains one of the main tools to distribute your libraries. If you are a maintainer of an open source library, it would be wise to keep supporting CocoaPods for the foreseeable future.

1. Building the XCFramework

Without going into much detail on how a XCFramework is built, below is the gist to build a fictitious StarWars.kit.

# iOS devices
xcodebuild archive \
    -scheme StarWarsKit \
    -archivePath "archives/StarWarsKit-iOS.xcarchive" \
    -destination "generic/platform=iOS" \
    -sdk iphoneos \
    SKIP_INSTALL=NO \
    BUILD_LIBRARY_FOR_DISTRIBUTION=YES

# iOS simulators
xcodebuild archive \
    -scheme StarWarsKit \
    -archivePath "archives/StarWarsKit-iOS-simulator.xcarchive" \
    -destination "generic/platform=iOS Simulator" \
    -sdk iphonesimulator \
    SKIP_INSTALL=NO \
    BUILD_LIBRARY_FOR_DISTRIBUTION=YES

xcodebuild -create-xcframework \
    -framework "archives/StarWarsKit-iOS.xcarchive/Products/Library/Frameworks/StarWarsKit.framework" \
    -framework "archives/StarWarsKit-iOS-simulator.xcarchive/Products/Library/Frameworks/StarWarsKit.framework" \
    -output "StarWarsKit.xcframework"

At this point we'll have the StarWarsKit.xcframework at the root of our project.

2. Publicly host the XCFramework

While you can host your binary anywhere and publish that via SPM, CocoaPods is a bit more strict and requires you to keep the built binaries with the Podspec.

In this case, it's best to keep the XCFramework in your git repository and push it along.

3. Creating the Podspec

At the root of the project, create the following StarWars.podspec. In this spec we're not publishing actual code, so we don't have to specify which files are applicable. Instead, we must use the vendored_frameworks property to indicate which XCFramework(s) need to be published.

Pod::Spec.new do |spec|
  spec.name               = "StarWars"
  spec.version            = "1.0.0"
  spec.summary            = "Star Wars Library for iOS apps"
  spec.description        = "..."
  spec.homepage           = "..."
  spec.documentation_url  = "..."
  spec.license            = { :type => "MIT" }
  spec.author             = { "Star Wars" => "..." }
  spec.source             = { :git => 'your repo here', :tag => "#{spec.version}" }
  spec.swift_version      = "5.3"

  # Supported deployment targets
  spec.ios.deployment_target  = "10.0"

  # Published binaries
  vendored_frameworks = "StarWarsKit.xcframework"
end

Final thoughts

Even though we now have SPM, CocoaPods is still widely used and will continue to be. Providing proper support for it remains as important as ever. Bringing XCFrameworks into the mix only improves certain pain points with directly compiling distributed code.

As always, we hope you liked this article and if you have anything to add, we are available via our Support Channel.

Keep up-to-date with the latest news