BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

Adding Realm as a dependency to an iOS Framework

Diego Freniche4 min read • Published Jun 29, 2021 • Updated Sep 02, 2022
Facebook Icontwitter iconlinkedin icon
Rate this tutorial

Adding Realm as a Dependency to an iOS Framework


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.

The Problem

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?

Solution 1: Use Xcode to Create the Framework and Add Realm with SPM

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.
Choosing a template for the project
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.
Adding Realm as a Package to the Project, step by step
In this case, we’ll add Realm Cocoa, a package that contains two libraries. We’re interested in Realm Swift: 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!

Solution 2: create the Framework using SPM and add the dependency directly in Package.swift

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

Creating the Package from Terminal

  • 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
Terminal showing the results of running above commands

Creating the Package using Xcode

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.
Xcode showing the newly created Package.swift

Adding Realm as a dependency

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 file
Here, 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.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial

Announcing the Realm C++ SDK Alpha

Apr 03, 2024 | 5 min read

Realm Meetup - Realm JavaScript for React Native Applications

Mar 21, 2023 | 32 min read

Filter Realm Notifications in Your iOS App with KeyPaths

May 17, 2022 | 2 min read

Realm Web SDK Tutorial

Jan 26, 2023 | 5 min read
Table of Contents
  • Adding Realm as a Dependency to an iOS Framework