5 minute quickstart

iOS Quickstart

Create an account

The first thing you need to do to get started with Firebase is sign up for a free account. A brand new Firebase app will automatically be created for you with its own unique URL ending in firebaseio.com. We'll use this URL to store and sync data.

CocoaPods Setup

CocoaPods is the easiest way to get started with the Firebase iOS SDK. If you're new to CocoaPods, visit their getting started documentation.

To initialize CocoaPods in your project, run the following commands:

$ cd your-project-directory
$ pod init
$ open -a Xcode Podfile # opens your Podfile in XCode

Once you've initialized CocoaPods, just add the Firebase Pod to your Podfile:

pod 'Firebase', '>= 2.3.2'

Then run the command:

$ pod install
$ open your-project.xcworkspace

Now you're ready to get started in XCode.

Alternative Setup

For instructions on including the Firebase framework and its dependencies manually or using Firebase with Ruby Motion, see the guide on alternative setup here.

Getting started in Xcode

Objective-C

Include the Firebase header in your app to get all the needed classes:

#import <Firebase/Firebase.h>

Swift

To use Swift with the iOS SDK we need to setup a bridging header. If you do not already have a bridging header, you can create one in Xcode by:

  1. If you have a Swift project, add a new Objective-C File to the project called Temp. If you have an Objective-C project, add a new Swift file called Temp.
  2. When prompted to configure an Objective-C bridging header, select "Yes."
  3. This creates the bridging header file, which ends in -Bridging-Header.h.
  4. Delete the Temp file you created from your project.
  5. Import Firebase into your bridging header.
#import <Firebase/Firebase.h>

This is the only import you need. You will now be able to use Firebase in any of your Swift files!

Read & Write Data

Writing data

Getting data into our Firebase database is a cinch. We first need to create a reference to our database using our URL endpoint. Once we have a reference, we can write any valid JSON object to it using setValue.

// Create a reference to a Firebase location
Firebase *myRootRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com"];
// Write data to Firebase
[myRootRef setValue:@"Do you have data? You'll love Firebase."];
// Create a reference to a Firebase location
var myRootRef = Firebase(url:"https://<YOUR-FIREBASE-APP>.firebaseio.com")
// Write data to Firebase
myRootRef.setValue("Do you have data? You'll love Firebase.")

Reading data

Firebase will stream data in realtime to any client that is listening. To setup a listener we need to call the observeEventType method. We will use the FEventTypeValue to listen for all changes. Then we'll add a block that returns a FDataSnapshot object that contains our data.

// Read data and react to changes
[myRootRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
  NSLog(@"%@ -> %@", snapshot.key, snapshot.value);
}];
// Read data and react to changes
myRootRef.observeEventType(.Value, withBlock: {
  snapshot in
  println("\(snapshot.key) -> \(snapshot.value)")
})
Call value on the FDataSnapshot returned from the callback to access the data.

In the example above, the value event will fire once for the initial state of the data, and then again every time the value of that data changes. You can learn more about the various event types and how to handle event data in our documentation on reading data.

Authenticate Your Users

Firebase provides full support for authenticating users with Email & Password, Facebook, Twitter, GitHub, Google, or your existing authentication system.

To get started with Email & Password auth, enable the Email & Password provider in your Firebase app's dashboard:

  1. Choose the Login & Auth tab.
  2. Select the Email & Password tab and enable authentication.

Now that the authentication provider is enabled you can create a new user:

[myRootRef createUser:@"bobtony@example.com" password:@"correcthorsebatterystaple"
    withValueCompletionBlock:^(NSError *error, NSDictionary *result) {

    if (error) {
        // There was an error creating the account
    } else {
        NSString *uid = [result objectForKey:@"uid"];
        NSLog(@"Successfully created user account with uid: %@", uid);
    }
}];
myRootRef.createUser("bobtony@example.com", password: "correcthorsebatterystaple",
    withValueCompletionBlock: { error, result in

    if error != nil {
        // There was an error creating the account
    } else {
        let uid = result["uid"] as? String
        println("Successfully created user account with uid: \(uid)")
    }
})

Once you've created your first user, you can log them in using the authUser method.

Learn how to authenticate via Facebook, Twitter, Google or your own custom system in our User Authentication guide.

Secure Your Firebase

Use our powerful expression-based Security and Firebase Rules to control access to your data and validate input:

{
  ".read": true,
  ".write": "auth.uid === 'admin'",
  ".validate": "newData.isString() && newData.val().length < 500"
}

Firebase enforces your Security and Firebase Rules consistently whenever data is accessed. The rules language is designed to be both powerful and flexible, so that you can maintain fine-grained control over your application's data.

What's Next?