firebase Getting started on iOS


Example

Prerequisites

  1. Create a new project and add an iOS app to that project in the Firebase Console.
  2. Download and include GoogleServices-Info.plist in your application.

Add Firebase Storage to your app

Add the following dependency to your project's Podfile:

pod 'Firebase/Storage'

Run pod install and open the created .xcworkspace file.

Follow these instructions to install Firebase without CocoaPods

Set up Firebase Storage

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 the following two steps.

Import the Firebase module:

// Obj-C
@import Firebase;
// Swift
import Firebase

Configure a FIRApp shared instance, typically in your application's application:didFinishLaunchingWithOptions: method:

// Obj-C
[FIRApp configure];  
// Swift
FIRApp.configure()  

Get a reference to the storage service, using the default Firebase App:

// Obj-C
FIRStorage *storage = [FIRStorage storage];  
// Swift
let storage = FIRStorage.storage()  

Create a reference to a file in Firebase Storage:

// Obj-C
FIRStorageReference *reference = [[storage reference] child:@"path/to/file.txt"];
// Swift
let reference = storage.reference().child("path/to/file.txt")

Upload a file to Firebase Storage:

// Obj-C
NSData *data = ... 
FIRStorageUploadTask *uploadTask = [riversRef putData:data metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    NSURL downloadURL = metadata.downloadURL;
  }
}];
// Swift
let data: NSData! = ...
let uploadTask = riversRef.putData(data, metadata: nil) { metadata, error in
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    let downloadURL = metadata!.downloadURL
  }
}