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.