How to make a Swift framework?

Creating a Swift framework shouldn't be hard. This tutorial will help you making a universal framework for complex projects.

2017/10/23Tools

What is a framework?

A framework is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library, nib files, image files, localized strings, header files, and reference documentation in a single package.

So in a nutshell, a framework is a highly reusable component for your apps.


How to make it?

There is an article about Xcode conventions which will help you organize your projects, you should check that first, if you haven't before.

Traditional way

There is a traditional way to make a framework in Xcode. I'm going to create a shared framework for all the apple platforms (iOS, macOS, tvOS, watchOS), which is going to be capable of logging things to the standard console.

Let's make the project:

Now in Finder:

Back to Xcode:

The goal is to achieve a structure somewhat like this:

Project settings:

Scheme settings:

NOTE: There is a flag in Xcode to allow app extension API only, if you are embedding your framework inside an application extension it should be enabled!

Congratulations, now you have your brand new Swift framework made in the traditional way. Let's continue with a neat trick.


Universal cross platform framework

It is possible to create a multiplatform single scheme Xcode project with cross platform support for every platform, but it's not recommended because it's a hack. However multiple open source libraries do the same way, so why shouldn't we.

NOTE: States can be mixed up if you are building for multiple platforms, however this is a nice clean way to support every platforms, without duplications.


How to use a Swift framework?

Embedding your framework is the most straightforward thing to do. You can simply drag the framework project to another Xcode project, the only thing left to do is to the embedded the framework into the application. You can go to the embedded binaries section inside the general project info tab and add the framework as a dependency.

Swift Package Manager

With SPM, you have to make a Package.swift file first, then you'll be able to build your targets with the swift build command. Now that Xcode supports the Swift Package Manager, it's really easy to integrate third party frameworks by using it.


You can download the final framework examples from GitHub.

Do you want to learn more?

Make sure that you don't miss out my deep dive into swift frameworks post.


External sources

Picture of Tibor Bödecs

Tibor Bödecs

Creator of http://theswiftdev.com (weekly Swift articles), server side Swift enthusiast, full-time dad. -- Follow me & feel free to say hi. 🤘🏻 -- #iOSDev #SwiftLang

Twitter · GitHub

📬

100% Swift news, delivered right into your mailbox

Subscribe to my monthly newsletter. On the first Monday of every month, you'll get an update about the latest Swift news including my articles and everything what happend in the Swift community as well.