Setting up Firebase

Setting up Firebase 🔥

Now it’s time for setting up Firebase, the service for retrieving and sending our messages to our database. For our purposes, Firebase is free of charge. The only thing you need is to setup up a Google account. So create one if you do not already have one.

Then visit Firebase and click on “Get started”, then on “Create a project “.

You’re now prompted with some instructions. Follow these to create your Firebase project.

After your Firebase project is created, click on the iOS icon to add Firebase to your Xcode project.

Let’s go through the necessary steps you are now prompted with:

Step 1: Enter the iOS bundle ID of your app. You can find your iOS bundle ID by opening your project’s settings.

Now copy and paste your Bundle Identifier to the Firebase console. You can leave “App nickname” and “App Store ID” empty. Then, click on “Register app”.

Step 2: Download the GoogleService-Info.plist file and drag it into your Xcode project’s project navigator. Make sure you check the “copy items if needed“ box that shows up. Then, quit Xcode by pressing CMD-Q.

Step 3: Now, we need to setup up cocoapods for your Xcode project. It’s a dependency manager and used for adding third-party functionality such as Firebase to an Xcode project. To add the corresponding pod file (the place where you install dependencies such as Firebase), open your macOS “Terminal” app and navigate to your Xcode project’s direction. You can do this by writing “cd” and dropping the folder that contains the project into the terminal. 

Then hit enter to navigate to that direction. Next, write “pod init” and hit return. Don’t close your terminal yet!

Your project’s folder should now contain a podfile. Open it and add the following lines of code to it:

pod 'Firebase/Core' 
pod 'Firebase/Storage' 
pod 'Firebase/Auth' 
pod 'Firebase/Database'

Save the pod file, write “pod install” into the terminal and hit enter. In Firebase, click on “Next”. Wait until the pods are installed. This may take a few minutes.

Step 4From now on, only use the newly created .xcworkspace project that you should now see inside your project’s folder. It should have a white icon. Your regular project doesn’t contain the Firebase dependencies!

The last step is to open your ChatMessengerApp.swift file. Import the Firebase library and add the following class to it:

import Firebase
import SwiftUI

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        return true
    }
}

//...

Finally, add the appDelegate property to complete the Firebase setup.

@main
struct ChapAppFinishedApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    //...
}

In your Firebase console, click on “Continue”.

Step 5: To see if the app is connected to Firebase, run it in the standard simulator, and take a look at your Firebase console. It can take some time until Firebase notices that your app is connected. If everything went successfully, click on “Continue to console”.

We almost finished the Firebase setup. The only thing left is to implement the database functionality, but that’s really easy.

In the left section of your Firebase console, click on “Build” -> “Realtime Database” and then on “Create Database “. Select any server location. Next, select “Start in test mode” and click on “Enable”.

Important: NEVER use these settings for an app to be deployed. These settings enable ANYONE to read out the database and to apply changes to it. For our purposes, this is fine. But be aware of this when writing a public app.

We’ve successfully set up our app for firebase for our chat app! Now we are ready to continue working on our app functionality!

Our database structure 🌳

Firebase databases can be described as tree-structured, containing flat paths instead of complex “table-style” schemes. Databases like this are also called “NoSQL” databases. The tree structure of our database will be straightforward:

The root of our database is the chat app itself. This root is already there by default. Take a look at the current realtime database.

Below our root node, we save our chat messages on the “chats” node. Every chat message on this node has the following properties: The sender’s username and the message’s content. Each chat message itself will be identified by a unique key.

You see that the root of our database and the chats node itself will always be the same. Only the chat messages below the chats node won’t be static. To access the root and the chat node, we can already declare the corresponding constants inside our Xcode project.

Therefore, create a new Swift file named “DatabaseConstants”, import the Firebase library and insert the following constants:

import Foundation
import Firebase

let databaseRoot = Database.database().reference()
let databaseChats = databaseRoot.child("chats")

The databaseRoot constant refers to the realtime database itself. The databaseChats constant creates a child out of that root called “chats”, our chats node!

Updating our data model 🛠

Since we are now ready to use our database, we can delete the sampleConversation array. 

At this point, we can also optimize our data model. Instead of manually checking for each initialized ChatMessage whether it’s sent by the user or someone else, we check the stored value at the UserDefault’s “username” key and compare it to the username of the particular chat message in our database. Therefore, we add an init function of our ChatMessage struct as follows.

struct ChatMessage: Hashable {
    //...
    
    init(messageText: String, username: String) {
        self.messageText = messageText
        self.userName = username
        if UserDefaults.standard.string(forKey: "username") == username {
            self.isMe = true
        } else {
            self.isMe = false
        }
    }
}

Accordingly, adapt the sampleConversation array.

let sampleConversation = [
    ChatMessage(messageText: "Hello how are you?", username: "Me"),
    ChatMessage(messageText: "I'm fine and you?", username: "Another user"),
    ChatMessage(messageText: "I'm fine as well. Thanks for asking. What are you doing right now?", username: "Me"),
    ChatMessage(messageText: "Do you have any vacation plans coming up?", username: "Another user"),
    ChatMessage(messageText: "I'm thinking about going to Spain", username: "Me"),
    ChatMessage(messageText: "What about you ?🤔", username: "Me"),
    ChatMessage(messageText: "Sounds great!", username: "Another user"),
    ChatMessage(messageText: "Thinking about flying to Sweden for christmas! 🎄", username: "Another user"),
    ChatMessage(messageText: "I would love to go to Sweden one day!", username: "Me")
]

5 replies on “Setting up Firebase”

Typo:
In the code sample below “… add the the appDelegate property…”
– there are two consecutive “the” in the accompanying sentence
– `struct ChapAppFinishedApp` looks like it should be `struct ChatAppFinishedApp`

Issues:
– In “ChatAppStarterApp.swift”, Xcode keeps telling me “No such module ‘Firebase'”. This is despite having done
1) writing `pod ‘Firebase’` in Podfile
2) running `pod install` in terminal

Question:
– The initialization code from Firebase includes `var window: UIWindow?`. Why does the tutorial exclude that line?

I am putting a chat into an app that I already has firebase pods (firestore, storage, auth), I just added two pods to my podfile database and core…

# Pods for Secret0
pod ‘Firebase/Analytics’
pod ‘Firebase/Firestore’
pod ‘Firebase/Auth’
pod ‘Firebase/Storage’
pod ‘Firebase/Database’
pod ‘Firebase/Core’

I used to initialize firebase like this:
init() {
FirebaseApp.configure()
}

anyways I initialized firebase the way you suggested and I’m getting an error “121 duplicate symbols for architecture arm64”

I assume I can use storage, firestore and database in the same project or why am I having the error?

I had an issue with my other firebase pods, so I had to run pod reintegrate and then pod install..

but now I’m getting a “Cannot use instance member ‘databaseRoot’ within property initializer; property initializers run before ‘self’ is available” with the 2nd constant.

keep in mind I’m using firestore, storage and real time db in my project

Trying to install pod using Terminal, I get the following:
willhites-imac:[Starter] ChatMessenger homedesktop$ pod init
-bash: pod: command not found

Leave a Reply

Your email address will not be published. Required fields are marked *