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
Once you've created an iOS or OS X application in XCode, you can import the Firebase client. CocoaPods is the easiest way to get started with the Firebase 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.5.1'
Then run the command:
$ pod install
$ open your-project.xcworkspace
Now you're ready to get started in XCode.
For instructions on including the Firebase framework and its dependencies manually 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
You can use Firebase in your Swift class by simply importing Firebase:
import Firebase
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 database URL
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)")
})
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:
- Choose the Login & Auth tab.
- Select the Email & Password tab and enable authentication.
Now that the authentication provider is enabled you can create a new user:
[myRootRef createUser:@"[email protected]" 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("[email protected]", 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?
- Read the Development Guide
- View the full iOS API
- Explore our code examples