Firebase Setup on iOS - hikaruhotta/password-iOS-app GitHub Wiki
Installation
- Add the dependency for Cloud Storage to your project's
Podfile
:
pod 'Firebase/Storage'
- Run pod install and open the created
.xcworkspace
file.
Setup
- Import firebase modules in
UIApplicationDelegate
.
import Firebase
import FirebaseDatabase
import FirebaseFunctions
- Configure a
FirebaseApp
shared instance and Firebase Authentication to create and use temporary anonymous accounts indidFinishLaunchingWithOptions
.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Auth.auth().signInAnonymously() { (authResult, error) in
}
return true
}
- Configure an instance of FIRDatabaseReference.
var ref: DatabaseReference?
ref = Database.database().reference()
- Configure an instance of FIRDatabaseHandle.
var databaseHandle: DatabaseHandle?
- 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 {
}
}
}