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.
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.
Before we proceed with the distribution process, meeting the following requirements or prerequisites is important.
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.
Open Terminal and navigate to your built frameworks’ directory. Run the following command to create the XCFramework:
#!/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.
Now that you have created and verified your XCFramework, it’s time to distribute it.
Turn Your App Ideas into Reality: Hire Our iPhone App Developer Now!
To distribute your XCFramework through CocoaPods, a few extra steps are involved. Here is how you can do it:
Before you start, ensure you abide by the following:
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
2.2 Create a basic podspec file
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
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
Replace “1.0.1” with the version number you want to tag.
2.5 Push the tag to your Git repository
2.6 Validate the podsec
Run the following command to validate your 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:
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).
Hire Mobile App Developers To Get A Unique Development Touch For Your iOS Apps
Distributing your XCFramework using Swift Package Manager includes a few additional steps. These are below:
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:
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
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:
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:
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.
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.
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.