Firebase SwiftUIDemo app
This app demonstrates how to use Firebase in a SwiftUI iOS app.
Firebase Setup
- Go to firebase.com
- Click new project.
- Copy app bundle identifier from the top-left file in Xcode.
- Create a new iOS app.
- Paste app bundle identifier as well as give it a name.
Pods Setup
- Open a terminal window in your project’s root.
- Type
pod init
- Click through the steps to add Firebase to your project.
- Choose the Firebase libraries you want to use to setup the project.
- Open with a text editor the new pod file.
vim Podfile
- Install the selected libraries
pod install
- Note: if you need to remove pods, then you do that through
pod install
as well after deleting
the library from thePodfile
Database Setup
In Firebase, there are two databases available:
- Firestore
- Realtime
The Firestore database is the latest available database, so we recommend this one, because it builds
on lessons learned from the older Realtime database. See
here for more details on the two.
You create the database in the top-left setting of the Firebase console under Build -> Firestore Database
When you first create the database, you get to choose a location. Note that you cannot change this location
once you create your app. Therefore, choose the location that most closely will serve your users. You can
see all of the available locations here
If you want a database, which goes global, then you should check out FaunaDB.
Database Interaction
We create an instance of the Firebase database within our project. This instance automatically uses our
GoogleService-Info.plist file in order to authenticate with our Firebase app. We must initialize this
database in the init method for our app.
init() {
FirebaseApp.configure()
// Create documents below
makeReservation()
}
func makeReservation() {
// Reference the Cloud Firestore database
let db = Firestore.firestore()
// Access the reservations collection, or creates it, if not already created
let reservations = db.collection("reservations")
// Create a document with a given identifier
reservations.document("test123").setData([
"name": "Carol",
"people": 22
])
// Create document with a unique identifier
reservations.document().setData(["name" : "Tom"])
// Create document with given data, then assign it to a constant
let document = reservations.addDocument(data: [
"name": "Sue",
"people": 10
])
// Work with the document
}