Create your first iOS framework

Atikur Rahman
Evangelist Apps Blog
4 min readJul 18, 2022

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' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

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' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

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:

LinkedIn: https://www.linkedin.com/company/evangelist-apps-limited/

Twitter: https://twitter.com/EvangelistSW/

--

--