A Comprehensive Guide to Creating XCFramework in iOS Development
The tools and methods at our disposal change along with the landscape of iOS development. Let me introduce you to XCFramework, Apple’s revolutionary packaging technology that completely changes the way we bundle resources and libraries for use on various platforms. Say goodbye to complexity and welcome to simplified dependency management.
This tutorial is your pass to becoming an expert with XCFramework and enhancing your projects, regardless of experience level. Now, let’s get going.
Table of Contents
- Creating a framework
- Export a framework
- Import a framework
Creating a Framework
Before diving into XCFramework creation, ensure your project is set up correctly and all dependencies are properly configured. You’ll need a class or library or framework that you want to package as an XCFramework. Make sure it’s compatible with multiple platforms (e.g., iOS, macOS, tvOS) and targets based on your needs.
Step 1: Create a new framework project.
Step 2: Provide required details, and I would suggest ticking ‘Include documentation’.
NOTE: Product name should not be same as class name, if class name is ‘TabBar’ then product name must be like ‘TabBarKit’ or ‘TabBarLib’.
Step 3: Add class file for component. Here I share a class file example which includes commenting as per swift commenting standards (Commenting ref). Documentation will be created based on these comments. (Documentation creation process in step 4)
Step 4: Go to the highlighted file which was shown in the below image and add the document regarding your functionality in markdown format.
Step 5: Click on the main project file > Targets > Build Settings Tab > Build Options Section > Change Build Libraries for Distribution from “No” to “Yes” as shown in the image below.
Step 6: Go to the Device Selection section, select device for the platform which you would like to build, as shown in the image.
To Support ‘iOS’:
- Select ‘any iOS Device’,
- Go to the Product section > click on build.
To Support ‘iOS Simulator’:
- Select ‘any iOS Simulator Device’,
- Go to the Product section > click on build.
To Support ‘Mac’:
- Select ‘any Mac’,
- Go to the Product section > click on build.
Export a Framework
Step 1: After completing the previous step, navigate to the below specified path and verify that the framework is created inside the folder for each platform you have selected in the above steps.
Go to the Xcode > Settings > Locations > click on the arrow next to the Derive Data Path > ProductName(your XCFrameworkFile Name) > Build > Product
Step 2: Open terminal and navigate to above products folder path and type below mentioned command to create xcframework.
xcodebuild -create-xcframework \
-framework Debug-iphoneos/PSSPLTabBarKit.framework \
-framework Debug-iphonesimulator/PSSPLTabBarKit.framework \
-framework Debug-maccatalyst/PSSPLTabBarKit.framework \
-output Output/PSSPLTabBarKit.xcframework
NOTE: Here folder name can be different as per your XCFramework file name, you can also follow above process by copying frameworks to another directory as well.
After following the above process, the XCFramework will be generated and ready to be used in your project.
Import a Framework
Step 1: Now navigate to your project where you want to use this xcframework. Click on the main project file, and in the General tab section, click “+” button in the Framework and Libraries section to add the generated framework select ‘Embed & Sign’ as shown in the below screenshot.
Step 2: Now import Module in your file where you want to use it by adding import statement and start using your framework.
Conclusion
Building an XCFramework in iOS allows you to create a versatile binary framework that can be easily integrated across different Apple platforms and architectures. By following the step-by-step guide outlined above, you can successfully create an XCFramework for your iOS project and streamline the distribution of your framework to other developers and projects.
Happy Reading!!