How to Build xcframework With Xcode

Ahmed Fathi
The Startup
Published in
5 min readSep 5, 2020

People build frameworks to help the community by building useful blocks of code and distribute it to others so they can use it directly without having to rewrite all that logic again.

It is very useful in the software industry, you can always rely on others and open source libraries/frameworks that other have invested a great time perfecting & testing for other to use, and we as software engineers we should leverage that when it is convenient for us.

In large teams, people build frameworks to modularize the work which helps large teams to split up the work and smaller teams can work in isolation. Also to be able to reuse the same component/framework in different apps within the company without having to rewrite or copy/paste that code from a place to place.

In this article I’ll cover how you can build your framework and test it using xcode.

Create the Framework source code

This article is written and tested by xcode Version 11.4.1 (11E503a).

Open xcode and start with a framework project.

I’ll name mine FruitBasket, you can choose whatever you want, and I’ll save it to my desktop so I can easily access it later when I’m building the xcframework.

What is FruitBasket? It is a silly example I made for this article it is basically a basket of fruits and we should be able to add fruits to it and the basket should tell us how many fruits are in there and what are they. Quite fun, isn’t it?

I’ll start by adding a swift file to contain simple enum for a number of fruits to use, I’ll name that file fruit.

As you can see I’ve added public access modifier to be able to access that enum from outside the framework, (ie from our project when we import this framework).

I’ll add a simple class for fruit basket. Feel free to add only one function to print Hello, world, It doesn’t really matter in this example.

Now the framework code is ready, let us generate the framework.

Here is thing that you need to know about xcframeworks, they are pre-compiled code (binary code) that you distribute for others.

Xcode builds framworks for specific architectures, so xcode will build framework for simulators and another one for iOS devices and another one for macOS and tvOS.

In most of the cases when you distribute your framework to your client you won’t give them separate frameworks and they replace when they run on simulator or iPhone devices, at least not in 2020 :D

Previously we would create all the frameworks that we need and then use LIPO tool to help us combine all these frameworks in one universal framework .framework and distribute it. In this link — Yousef Hamza has wrote a very detailed article on how to do that using LIPO.

But since apple has introduced the new xcframework, we don’t need LIPO anymore.

So let us see how to create it.

Generating the .xcframwork for simulators and devices

Start by enabling Build Libraries for Distribution from build settings in xcode.

Then, open up a terminal window and navigate to your project directory, in my case it is saved on my desktop

NOTE: All the following commands are for FruitBasket if you named you project something differently you have to adjust all of them with you framework name.

cd ~/Desktop/FruitBasket/

and write the following command, which will create a framework that can run on simulators.

xcodebuild archive \
-scheme FruitBasket \
-archivePath ~/Desktop/FruitBasket-iphonesimulator.xcarchive \
-sdk iphonesimulator \
SKIP_INSTALL=NO

Wait for it complete and you can see the success message.

Check you desktop and you should see the xcarchive has been created there with the name FruitBasket-iphonesimulator.xcarchive.

Then write this very similar command to create another one for iOS devices.

xcodebuild archive \
-scheme FruitBasket \
-archivePath ~/Desktop/FruitBasket-iphoneos.xcarchive \
-sdk iphoneos \
SKIP_INSTALL=NO

After success now you should have the two xcarchives same as me

From there we need to combine both in one xcframework , copy and paste the follow command to your terminal and hit enter

xcodebuild -create-xcframework \
-framework ~/Desktop/FruitBasket-iphonesimulator.xcarchive/Products/Library/Frameworks/FruitBasket.framework \
-framework ~/Desktop/FruitBasket-iphoneos.xcarchive/Products/Library/Frameworks/FruitBasket.framework \
-output ~/Desktop/FruitBasket.xcframework

You should get that the xcframwork has been written successfully.

Check your desktop and look for YourFrameworkName.xcframework

Testing the framework

Create a new project from xcode, single view app is okay for testing.

Add your xcframework file you have just created by dragging and dropping it in Framworks, Libraries, and Embedded Content

Now switch over to your view controller and import the framework and start testing it.

Run it on both simulator and real device and it should run and give you the same results.

Congratulations on creating your first xcframework that runs on both simulators and devices.

Github repository for FruitBasket

What’s next?
To know more about binary framework & their interfaces and how they work watch this awesome WWDC video https://developer.apple.com/videos/play/wwdc2019/416/

In next Article I’ll explain how to distribute your xcframwork using Cocoapods, and also who to distribute as a private pod for only your clients.

Thank you for taking the time.
All you comments and ideas are welcomed.
AhmedFathi680@gmail.com
LinkedIn
Github

--

--