Adding Realm as a dependency to an iOS Framework
Rate this tutorial
In this post we’ll review how we can add RealmSwift as a dependency to our libraries, using two different methods: Xcode assistants and the Swift Package Manager.
I have a little, nice Binary Tree library. I know that I will use it for a later project, and that it'll be used at least in a macOS and iOS app. Maybe also a Vapor web app. So I decided to create a Framework to hold this code. But some of my model classes there need to be persisted in some way locally in the phone later. The Realm library is perfect for this, as I can start working with regular objects and store them locally and, later, if I need a really simple & quick to implement backend solution I can use Atlas Device Sync.
But the problem is, how do we add Realm as a dependency in our Frameworks?
The first way to create the Framework is just to create a new Xcode Project. Start Xcode and select
File > New > Project
. In this case I’ll change to the iOS tab, scroll down to the Framework & Library section, then select Framework. This way I can share this Framework between my iOS app and its extensions, for instance.
Now we have a new project that holds our code. This project has two targets, one to build the Framework itself and a second one to run our Unit Tests. Every time we write code we should test it, but this is especially important for reusable code, as one bug can propagate to multiple places.
To add Realm/Swift as a dependency, open your project file in the File Navigator. Then click on the Project Name and change to the Swift Packages tab. Finally click on the + button to add a new package.

In this case, we’ll add Realm Cocoa, a package that contains two libraries. We’re interested in Realm Swift: https://github.com/realm/realm-cocoa. We want one of the latest versions, so we’ll choose “Up to major version” 10.0.0. Once the resolution process is done, we can select RealmSwift.
Nice! Now that the package is added to our Framework we can compile our code containing Realm Objects without any problems!
The other way to author a framework is to create it using the Swift Package Manager. We need to add a Package Manifest (the Package.swift file), and follow a certain folder structure. We have two options here to create the package:
- Use the Terminal
- Use Xcode
- Open Terminal / CLI
- Create a folder with
mkdir yourframeworkname
- Enter that folder with
cd yourframeworkname
- Run
swift package init
- Once created, you can open the package with
open Package.swift

You can also use Xcode to do all this for you. Just go to
File > New > Swift Package
, give it a name and you’ll get your package with the same structure.
So we have our Framework, with our library code and we can distribute it easily using Swift Package Manager. Now, we need to add Realm Swift. We don’t have the nice assistant that Xcode shows when you create the Framework using Xcode, so we need to add it manually to
Package.swift
The complete
Package.swift
fileHere, we declare a package named “BinaryTree”, supporting iOS 14
As this is a library, we declare the products we’re going to build, in this case it’s just one target called
BinaryTree
.Now, the important part: we declare Realm as a dependency in our library. We’re giving this dependency the short name “Realm” so we can refer to it in the next step.
In our target, we use the previously defined
Realm
dependency.And that’s all! Now our library can be used as a Swift Package normally, and it will include automatically Realm.
In this post we’ve seen different ways to create a Framework, directly from Xcode or as a Swift Package, and how to add
Realm
as a dependency to that Framework. This way, we can write code that uses Realm and distribute it quickly using SPM.In our next post in this series we’ll document this library using the new Documentation Compiler (DocC) from Apple. Stay tuned and thanks for reading!
If you have questions, please head to our developer community website where the Realm engineers and the Realm/MongoDB community will help you build your next big idea with Realm and MongoDB.