Create your first iOS framework

In this article, we will see how to build a framework for iOS platform.

Why would we build an iOS framework?

By creating an iOS framework, we can share code between apps or distribute it as a third-party library. It’s also a great way to modularize our code, which is very helpful for large teams. By modularizing our code, we can split up the work, where smaller teams can work in isolation.

Creating the framework

We’ll start by creating a framework. Open Xcode and start with a framework project.

Next, we choose options for our new framework project and then create the project.

Now that our framework project is ready, we will add a new Swift file. Naming isn’t important, let’s call it Utils.swift, as it will contain some utility functions.

To keep it simple, we will add just one function.

You might have already noticed that the function is marked with public access modifier. That’s required because this function will be accessed from another module which will import this framework.

After adding all the source code to our framework, we need to change a Build Settings. Select the project file and then the Build Settings tab. Under Build Options, we need to set the Build Libraries for Distribution to YES.

The nextstep is to Archive the framework.

Archiving the framework

We will need to run few commands from Terminal. Let’s open our terminal and navigate to the framework directory. I saved my framework project to Desktop.

cd Desktop/MyFirstFramework

We will be creating two different archives — one that will be working on real iOS devices and one that will be working in the Simulator. The project will be almost identical. Let’s create the first one — for iOS. Run the following command on your terminal -

xcodebuild archive \
-scheme MyFirstFramework \
-configuration Release \
-destination 'generic/platform=iOS' \
-archivePath './build/MyFirstFramework.framework-iphoneos.xcarchive' \

This will generate an archive of our framework for iOS.

Next, we will run the following command to make an archive for Simulators -

xcodebuild archive \
-scheme MyFirstFramework \
-configuration Release \
-destination 'generic/platform=iOS Simulator' \
-archivePath './build/MyFirstFramework.framework-iphonesimulator.xcarchive' \

We can verify that two different archives are generated by having a look at the build folder inside our project directory.

Finally, we are ready to build the binary framework, XCFramework.

Generating the XCFramework

This step is really simple, just run the following command on your terminal -

xcodebuild -create-xcframework \
-framework './build/MyFirstFramework.framework-iphoneos.xcarchive/Products/Library/Frameworks/MyFirstFramework.framework' \
-framework './build/MyFirstFramework.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/MyFirstFramework.framework' \
-output './build/MyFirstFramework.xcframework'

This command will generate the XCFramework to the build folder -

Now we are done building the framework, it’s time to use our framework.

Using the XCFramework Into Our Project

Let’s create a new SwiftUI app project -

Select the project file. Now drag the XCFramework to the Frameworks, Libraries and Embedded Content section of your project target -

Now we are ready to import functionalities from our framework into our app project.

Select ContentView.swift file and import MyFirstFramework.

Now we can use all the public types from the framework. Let’s use the welcome() function (line 13)-

Resume the preview and you will see the updated text!

Please share your thoughts and suggestions on this article.

Keep up with us on social media:





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store