Install Realm for iOS, macOS, tvOS, and watchOS¶
Overview¶
MongoDB Realm's Apple platform SDK enables your iOS, macOS, tvOS, and watchOS applications to interact with MongoDB Realm using either the Swift or Objective-C programming languages. This page details how to install Realm in your project and get started.
Prerequisites¶
Before getting started, ensure your development environment meets the following prerequisites:
Installation¶
Follow the procedure for either SwiftPM
or CocoaPods
below to add the
MongoDB Realm Apple platform SDK to your project.
SwiftPM¶
Add Package Dependency¶
In Xcode, select File
> Swift Packages
> Add Package Dependency
.
Specify the Repository¶
Copy and paste the following into the search/input box, then click Next
.
https://github.com/realm/realm-cocoa.git
Specify Options¶
Leave the default value of Up to Next Major
, then click Next
.
Select the Package Products¶
Select both Realm
and RealmSwift
, then click Finish
.
CocoaPods¶
Update the CocoaPods repositories¶
On the command line, run pod repo update
to ensure
CocoaPods can access the latest available Realm versions.
Initialize CocoaPods for Your Project¶
If you do not already have a Podfile for your project,
run pod init
in the root directory of your project to
create a Podfile for your project. A Podfile allows you
to specify project dependencies to CocoaPods.
Add Realm as a Dependency in Your Podfile¶
Add the line use_frameworks!
if it is not
already there.
Add the line pod 'RealmSwift', '=10.1.4'
to your main and test
targets.
When done, your Podfile should look something like this:
platform :ios, '12.0' target 'MyRealmProject' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for MyRealmProject pod 'RealmSwift', '=10.1.4' end
Install the Dependencies¶
From the command line, run pod install
to fetch the
dependencies.
Use the CocoaPods-Generated .xcworkspace
File¶
CocoaPods generates an .xcworkspace
file for you. This
file has all of the dependencies configured. From now on,
open this file -- not the .xcodeproj
file -- to work
on your project.
Import Realm¶
Add the following line at the top of your source files to use MongoDB Realm:
import RealmSwift