Firebase Setup on iOS - hikaruhotta/password-iOS-app GitHub Wiki

Installation

  1. Add the dependency for Cloud Storage to your project's Podfile:
pod 'Firebase/Storage'
  1. Run pod install and open the created .xcworkspace file.

Setup

  1. Import firebase modules in UIApplicationDelegate.
import Firebase
import FirebaseDatabase
import FirebaseFunctions
  1. Configure a FirebaseApp shared instance and Firebase Authentication to create and use temporary anonymous accounts in didFinishLaunchingWithOptions.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    Auth.auth().signInAnonymously() { (authResult, error) in
    }
    return true
}
  1. Configure an instance of FIRDatabaseReference.
var ref: DatabaseReference?
ref = Database.database().reference()
  1. Configure an instance of FIRDatabaseHandle.
var databaseHandle: DatabaseHandle? 
  1. Initialize the client SDK to allow the client to call cloud functions.
lazy var functions = Functions.functions()

Database Listeners

Triggered once for each existing child and then again every time a new child is added to the specified path.

ref?.child(path).observe(.childAdded) { (snapshot) in
    var key = snapshot.key
    var value = snapshot.value as ? [Any]
}

Triggered any time a child node is modified.

ref?.child(path).observe(.childChanged) { (snapshot) in
    var key = snapshot.key
    var value = snapshot.value as ? [Any]
}

Triggered once when the child is created.

ref?.child("/lobbies/\(LOCAL.lobby!.lobbyId)/public").child(key).observeSingleEvent(of: .value, with: { (snapshot) in          
    var key = snapshot.key
    var value = snapshot.value as ? [Any]
}) { (error) in
    print(error.localizedDescription)
}

Calling Cloud Functions

functions.httpsCallable(functionName).call([parameters]) { (result, error) in
    if let error = error as NSError? {
        if error.domain == FunctionsErrorDomain {
        }
    } 
}