Join us for Firebase Summit on November 10, 2021. Tune in to learn how Firebase can help you accelerate app development, release with confidence, and scale with ease. Register

Installation & Setup on Apple platforms

The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps with our Android, iOS, and JavaScript SDKs, all of your clients share one Realtime Database instance and automatically receive updates with the newest data.

Firebase Realtime Database is available on all Apple platforms, including iOS, macOS, macOS Catalyst, tvOS, and watchOS. The setup instructions in this page reference iOS in specific examples, but are generic and work for any Apple platform target.

Prerequisites

  1. Install the Firebase SDK.
  2. Add your app to your Firebase project in the Firebase console.

Create a Database

  1. Navigate to the Realtime Database section of the Firebase console. You'll be prompted to select an existing Firebase project. Follow the database creation workflow.

  2. Select a starting mode for your Firebase Security Rules:

    Test mode

    Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Understand Firebase Realtime Database Rules section.

    To get started with the web, iOS, or Android SDK, select test mode.

    Locked mode

    Denies all reads and writes from mobile and web clients. Your authenticated application servers can still access your database.

  3. Choose a region for the database. Depending on your choice of region, the database namespace will be of the form <databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app. For more information, see select locations for your project.

  4. Click Done.

When you enable Realtime Database, it also enables the API in the Cloud API Manager.

Add Firebase Realtime Database to your app

  1. Add the dependency for Firebase Realtime Database to your project's Podfile:

    pod 'Firebase/Database'
    
  2. Run pod install and open the created .xcworkspace file.

Configure Realtime Database Rules

The Realtime Database provides a declarative rules language that allows you to define how your data should be structured, how it should be indexed, and when your data can be read from and written to.

Set up Firebase Realtime Database

You must initialize Firebase before any Firebase app reference is created or used. If you have already done this for another Firebase feature, you can skip this step.

  1. Import the Firebase module in your UIApplicationDelegate:

    Swift

    import Firebase

    Objective-C

    @import Firebase;
  2. Configure a FirebaseApp shared instance, typically in your app's application:didFinishLaunchingWithOptions: method:

    Swift

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];

Then, create a reference to your database and specify the location you want to write to.

Swift

var ref: DatabaseReference!

ref = Database.database().reference()

Objective-C

@property (strong, nonatomic) FIRDatabaseReference *ref;

self.ref = [[FIRDatabase database] reference];

Next Steps