Quick Summary

In the world of Swift development, say goodbye to the hassle of copying and pasting the same code for every new project. XCFrameworks provides a simple solution for making your code modular and reusable across different Apple platforms like iOS and macOS. It’s the go-to method for sharing pre-compiled code. This blog post will walk you through the easy steps of distributing XCFrameworks with CocoaPods and Swift Package Manager.

Table of Contents

What is XCFramework?

Before we start with our steps to Distributing XCFrameworks with CocoaPods and Swift Package Manager, let us first look at its details. XCFramework is a distribution format tailored for binary frameworks across various Apple platforms, such as iOS, macOS, watchOS, and tvOS. It streamlines the distribution of libraries and frameworks by packaging them into a single bundle, providing a seamless integration experience for Xcode projects. This format simplifies sharing precompiled code, resources, and headers, offering a convenient solution for developers working across different Apple devices.

Prerequisites To Distributing XCFramework

Before we proceed with the distribution process, meeting the following requirements or prerequisites is important.

  • Xcode and Command Line Tools: Ensure you have the latest version of Xcode installed on your development machine. Additionally, install the Xcode Command Line Tools by running ‘xcode-select –install’ in the terminal.
  • A Swift project with the code you want to package into the framework.

Steps To Proceed With The Distribution of XCFramework

Step 1: Build the Frameworks

Open your Swift project in Xcode and build the frameworks for each platform you want to support (iOS, macOS, watchOS, tvOS). Ensure that the frameworks are built for both device and simulator architectures.

Step 2: Create the XCFramework

Open Terminal and navigate to your built frameworks’ directory. Run the following command to create the XCFramework:

Copy Text
#!/bin/bash
# Remove the old /archives folder
rm -rf archives
# iOS Simulators
xcodebuild -quiet archive \
    -scheme YourFramework \
    -destination "generic/platform=iOS Simulator" \
    -archivePath "archives/"YourFramework-iOS-simulator.xcarchive" \
    -sdk iphonesimulator \
    ONLY_ACTIVE_ARCH=NO \
    SKIP_INSTALL=NO \
    BUILD_LIBRARY_FOR_DISTRIBUTION=YES
# iOS Devices
xcodebuild -quiet archive \
    -scheme YourFramework \
    -archivePath "archives/YourFramework-iOS.xcarchive" \
    -destination "generic/platform=iOS" \
    -sdk iphoneos \
    ONLY_ACTIVE_ARCH=NO \
    SKIP_INSTALL=NO \
    BUILD_LIBRARY_FOR_DISTRIBUTION=YES
# Build YourFramework.xcframework
xcodebuild -create-xcframework \
    -framework "archives/YourFramework-iOS.xcarchive/Products/Library/Frameworks/YourFramework.framework" \
    -framework "archives/YourFramework-iOS-simulator.xcarchive/Products/Library/Frameworks/YourFramework.framework" \
    -output "archives/YourFramework.xcframework"

Replace “YourFramework” with the name of your framework and adjust the paths accordingly.

Step 3: Verify the XCFramework

  • Open Xcode.
  • Create a new project or open an existing one.
  • Drag and drop the newly created XCFramework into your project’s navigator.
  • In your project settings, select the target, go to “General,” and add the XCFramework under the “Frameworks, Libraries, and Embedded Content” section.
  • Build and run your project to ensure the XCFramework is correctly integrated.

Step 4: Distribute Your XCFramework

Now that you have created and verified your XCFramework, it’s time to distribute it.

  • Documentation: Provide clear documentation on integrating your XCFramework into other projects.
    Versioning: Consider adopting a versioning system to manage updates and changes.
  • Distribution Platforms: Choose a distribution platform that suits your needs (e.g., GitHub releases, CocoaPods, Carthage, Swift Package Manager).

Turn Your App Ideas into Reality: Hire Our iPhone App Developer Now!

Distribute Your XCFramework Using Cocoapods

To distribute your XCFramework through CocoaPods, a few extra steps are involved. Here is how you can do it:

Prerequisites

Before you start, ensure you abide by the following:

  • A CocoaPods account (you can create one on CocoaPods.org).
  • The XCFramework you want to distribute.
  • A valid version number for your framework.

Steps To Distribute XCFramework Using Cocoapods

Step 1: Prepare Your XCFramework

Ensure that your XCFramework is structured correctly. It should contain the framework for each platform you want to support.

Step 2: Create a Podspec File

2.1 Navigate to the root directory of your project in the terminal

Copy Text
cd /path/to/YourFrameworkProject 

2.2 Create a basic podspec file

Copy Text
pod spec create YourFramework

This command will create a basic template for a podspec file named YourFramework.podspec.

2.3 Edit the podspec file

Open the newly created podspec file in a text editor of your choice. Customize the podspec according to your framework’s details. Here’s an example using

Copy Text
Pod::Spec.new do |spec|
  spec.name         = 'YourFramework'
  spec.version      = '1.0.0'
  spec.summary      = 'A brief description of YourFramework.'
  spec.description  = 'A more detailed description of YourFramework.'
  spec.homepage     = 'https://yourwebsite.com/YourFramework'
  spec.license      = { :type => 'MIT', :file => 'LICENSE' }
  spec.author       = { 'Your Name' => '[email protected]' }
  spec.source       = { :git => 'https://github.com/yourusername/YourFramework.git', :tag => spec.version.to_s }

  # Specify the platform and supported versions
  spec.ios.deployment_target      = '10.0'
  spec.osx.deployment_target      = '10.12'
  spec.watchos.deployment_target  = '3.0'
  spec.tvos.deployment_target     = '10.0'

  # Specify the source files
  spec.source_files = 'YourFramework.framework/Headers/**/*.h'

  # Specify the vendored framework
  spec.vendored_frameworks = 'YourFramework.framework'
end

2.4 Create a new Git tag

Copy Text
git tag -a 1.0.1

Replace “1.0.1” with the version number you want to tag.

2.5 Push the tag to your Git repository

Copy Text
git push origin 1.0.1

2.6 Validate the podsec

Run the following command to validate your podspec:

Copy Text
pod lib lint YourFramework.podspec

Address any issues or warnings reported by the linter.

2.7 Push to CocoaPods

Once the podspec is validated, push your XCFramework to CocoaPods:

Copy Text
pod trunk push YourFramework.podspec

Make sure your CocoaPods account is set up and you are logged in ($ pod trunk register [email protected] ‘User name’ –description=’macbook air’ if not).

Push to CocoaPods
Let Us Shape Mobile App Brilliance Together!

Hire Mobile App Developers To Get A Unique Development Touch For Your iOS Apps

Distribute XCFramework Using Swift Package Manager

Distributing your XCFramework using Swift Package Manager includes a few additional steps. These are below:

Prerequisites

  • Xcode with Swift 5.3 or later.
  • A Swift project with the code you want to package into the framework.

Steps To Distributing your XCFramework using Swift Package Manager

Step 1: Build the XCFramework

You can build the XCFramework for the platforms you want to support.

Step 2: Create the Package Structure

Create a directory structure for your Swift package. In the root directory, create a file named Package.swift with the following content:

Copy Text
import PackageDescription

let package = Package(
    name: "YourFramework",
    platforms: [
        .iOS(.v12),   // Adjust the version as needed
        .macOS(.v10_12),
        // Add other platforms (watchOS, tvOS) as needed
    ],
    products: [
        .library(name: "YourFramework", targets: ["YourFramework"]),
    ],
    targets: [
        .binaryTarget(
            name: "YourFramework",
            url: "https://example.com/path/to/YourFramework.xcframework.zip", // Replace with the actual URL
            checksum: "sha256:yourchecksum"  // Replace with the actual checksum
        ),
    ]
)

Step 3: Archive and Distribute

3.1 Zip the XCFramework:

Zip the XCFramework for distribution. In the directory containing the .xcarchive files, run

Copy Text
zip -r YourFramework.xcframework.zip YourFramework-iOS.xcarchive/Products/Library/Frameworks/YourFramework.framework YourFramework-macOS.xcarchive/Products/Library/Frameworks/YourFramework.framework

3.2 Host the Zip File:

Host the generated zip file (e.g., on a server, GitHub release, or any other hosting service). Make sure to update the URL in your Package.swift accordingly.

3.3 Push to Git:

If you haven’t already, commit and push your changes to your Git repository.

Step 4: Consuming the Package

In another Swift project, you can now add the package dependency. In your Package.swift file:

Copy Text
import PackageDescription

let package = Package(
    name: "YourProject",
    dependencies: [
        .package(url: "https://example.com/path/to/YourFramework.git", from: "1.0.0"), // Replace with the actual URL and version
    ],
    targets: [
        .target(
            name: "YourProject",
            dependencies: ["YourFramework"]),
    ]
)

Then run:

Copy Text
swift package update

Now you can use import YourFramework in your Swift code.

To distribute an XCFramework via Swift Package Manager, host the XCFramework zip file and reference it in the Package.swift file. Ensure precise specification of the package URL and version. This ensures users can effortlessly incorporate your framework as a dependency in their Swift Package Manager projects.

Conclusion

In essence, this blog post acts as an in-depth manual, offering valuable insights into distributing XCFrameworks with CocoaPods and Swift Package Manager efficiently. This streamlines the integration of frameworks within the Apple ecosystem, enabling the creation of robust applications. For business owners searching for a mobile app development company, partnering with a dependable one guarantees a seamless journey and access to expert solutions. Embrace simplicity to stay at the forefront of technology.

Make A Move Towards A Successful iOS App With Our Mobile App Development Company.

Contact Us Today!

Build Your Agile Team

Hire Skilled Developer From Us

[email protected]

Your Success Is Guaranteed !

We accelerate the release of digital product and guaranteed their success

We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.

How Can We Help You?