0
0
iOS Swiftmobile~20 mins

Firebase setup for iOS in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Firebase Setup Screen
This screen helps you verify that Firebase is correctly set up in your iOS app by showing a welcome message after initialization.
Target UI
-------------------------
| Firebase Setup Screen  |
|-----------------------|
|                       |
|  Welcome to Firebase!  |
|                       |
|  [Check Connection]    |
|                       |
-------------------------
Add Firebase SDK initialization in the AppDelegate.
Create a simple UIViewController with a label that says 'Welcome to Firebase!'
Add a button labeled 'Check Connection' that when tapped, prints 'Firebase is configured' to the console.
Ensure the app imports Firebase and configures it properly.
Starter Code
iOS Swift
import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // TODO: Initialize Firebase here
    return true
  }

  // MARK: UISceneSession Lifecycle
  func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  }

  func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  }
}

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    // TODO: Add label and button here
  }
}
Task 1
Task 2
Task 3
Task 4
Solution
iOS Swift
import UIKit
import Firebase

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

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

  func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  }

  func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  }
}

class ViewController: UIViewController {

  private let welcomeLabel: UILabel = {
    let label = UILabel()
    label.text = "Welcome to Firebase!"
    label.textAlignment = .center
    label.font = UIFont.systemFont(ofSize: 24, weight: .medium)
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
  }()

  private let checkButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Check Connection", for: .normal)
    button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .regular)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
  }()

  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white

    view.addSubview(welcomeLabel)
    view.addSubview(checkButton)

    NSLayoutConstraint.activate([
      welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
      welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -20),
      checkButton.topAnchor.constraint(equalTo: welcomeLabel.bottomAnchor, constant: 20),
      checkButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
    ])

    checkButton.addTarget(self, action: #selector(checkConnectionTapped), for: .touchUpInside)
  }

  @objc private func checkConnectionTapped() {
    print("Firebase is configured")
  }
}

We start by importing Firebase and calling FirebaseApp.configure() in the AppDelegate to initialize Firebase when the app launches.

In the ViewController, we add a label centered on the screen that welcomes the user to Firebase.

Below the label, we add a button labeled 'Check Connection'. When tapped, it prints a confirmation message to the console, showing Firebase is set up correctly.

We use Auto Layout constraints to center the label and position the button below it. This simple UI confirms Firebase initialization visually and via console output.

Final Result
Completed Screen
-------------------------
| Firebase Setup Screen  |
|-----------------------|
|                       |
|  Welcome to Firebase!  |
|                       |
|  [Check Connection]    |
|                       |
-------------------------
Tapping 'Check Connection' prints 'Firebase is configured' in the Xcode console.
Stretch Goal
Add a label below the button that updates to 'Connection OK' when the button is tapped.
💡 Hint
Create a UILabel below the button and update its text inside the button's action method.