๐Ÿ”ฅFirebase ์‹œ์ž‘ํ•˜๊ธฐ

7201 ๋‹จ์–ด swiftuiswiftFirebaseFirebase

๐Ÿ”ฅFirebase?

ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋ž€ 2011๋…„ ํŒŒ์ด์–ด๋ฒ ์ด์Šค(Firebase, Inc)์‚ฌ๊ฐ€ ๊ฐœ๋ฐœํ•˜๊ณ  2014๋…„ ๊ตฌ๊ธ€์— ์ธ์ˆ˜๋œ ๋ชจ๋ฐ”์ผ ๋ฐ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ํ”Œ๋žซํผ์ด๋‹ค. ์•ฑ์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์„ APIํ˜•ํƒœ๋กœ ์ œ๊ณตํ•˜๊ธฐ์— ๊ฐœ๋ฐœ ๊ธฐ๊ฐ„์„ ๋‹จ์ถ•์‹œํ‚ค๋ฉฐ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋‹ค.

0. ์‹œ์ž‘ํ•˜๊ธฐ

์•„๋ž˜์˜ ๋งํฌ๋กœ ๋“ค์–ด๊ฐ€ ์‹œ์ž‘ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค.

Firebase

1. ์•ฑ ๋“ฑ๋ก

iOS๋ฅผ ์„ ํƒํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ๋ฅผ ํด๋ฆญํ•œ ์ดํ›„ ๋ฒˆ๋“ค ์•„์ด๋””๋ฅผ ๋ณต์‚ฌํ•˜๊ณ  ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ฑ์„ ๋“ฑ๋กํ•œ๋‹ค.

2. ๊ตฌ์„ฑ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

p.list ํŒŒ์ผ ๋‹ค์šด๋ฐ›๋Š”๋‹ค. ์ž‘์—… ํด๋” ์•ˆ์— ๋„ฃ๊ณ , ํ”„๋กœ์ ํŠธ์—์„œ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. ๋‚˜๋Š” Info.plist ์œ„์— ๋‘์—ˆ๋‹ค.

3. Firebase SDK ์ถ”๊ฐ€

Install Firebase with Swift Package Manager

cocoapods๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. Xcode์—์„œ File โ†’ Swift Packages โ†’ Add Packages๋กœ ๋“ค์–ด๊ฐ€ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ณต๋ถ™ํ•œ๋‹ค.

https://github.com/firebase/firebase-ios-sdk.git

์ดํ›„ ์‚ฌ์šฉํ•  ํŒจํ‚ค์ง€๋ฅผ ์ฒดํฌํ•˜๊ณ  ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค. ๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒดํฌํ•˜์˜€๋‹ค. ์ดํ›„์— ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ, ๋„ˆ๋ฌด ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

FirebaseAuth
FirebaseFirestore
FirebaseFunctions
FirebaseStorage

4. ์ดˆ๊ธฐํ™” ์ฝ”๋“œ ์ถ”๊ฐ€

SwiftUI ํŒŒ์ผ์—์„œ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ถ”๊ฐ€ํ•˜์ง€? ๋ง‰๋ง‰ํ•œ ์ƒ๊ฐ์ด ๋“ค์—ˆ์ง€๋งŒ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค. ํ”„๋กœ์ ํŠธ์ด๋ฆ„App.swift ํŒŒ์ผ์„ ์ฐพ๋Š”๋‹ค. ํŒŒ์ด์–ด๋ฒ ์ด์Šค๋ฅผ ๋ถˆ๋Ÿฌ์™€ init ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

import SwiftUI
import Firebase // Firebase ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

@main
struct ํ”„๋กœ์ ํŠธ์ด๋ฆ„App: App {

    init() {
        FirebaseApp.configure() // ์ฝ”๋“œ ์ถ”๊ฐ€
    }
    
    var body: some Scene {
        WindowGroup {
            MainTabView()
        }
    }
}

Swift ํŒŒ์ผ์ด๋ผ๋ฉด ๊ธฐ๋ณธ AppDelegate ํด๋ž˜์Šค์— ์•„๋ž˜์˜ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

import UIKit
import Firebase // Firebase ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(_ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions:
      [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure() // ์ฝ”๋“œ ์ถ”๊ฐ€
    return true
  }
}

Authentication

๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฅผ ํ—ˆ์šฉํ•˜์˜€๋‹ค. ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์—†๋Š” ๋กœ๊ทธ์ธ์€ ํ—ˆ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค์ง€ ์•Š์•˜๋‹ค.

FireStore Database

๋น ๋ฅธ ์„ค์ •์„ ์œ„ํ•ด ํ…Œ์ŠคํŠธ ๋ชจ๋“œ์—์„œ ์‹œ์ž‘ํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  FireStore ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ์œ„์น˜๋ฅผ ์„ค์ •ํ•œ๋‹ค. ์—ฌ๊ธฐ๋Š” ํ•œ๊ตญ์ด๋ฏ€๋กœ asia-northeast3(์„œ์šธ)๋กœ ์„ค์ •ํ•˜์˜€๋‹ค. ์ด ์œ„์น˜๋ฅผ ์„ค์ •ํ•œ ํ›„์—๋Š” ๋‚˜์ค‘์— ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ฒดํฌํ•˜์ž!๐Ÿง

Storage

์ดํ›„ ์œ ์ €์˜ ํ”„๋กœํ•„ ์‚ฌ์ง„์„ ๋„ฃ์„ ์ €์žฅ์†Œ์ด๋‹ค. ์ถ”๊ฐ€๋กœ Storage โ†’ rules์—์„œ ๋ณด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค. auth๊ฐ€ ์—†๋‹ค๋ฉด read, write๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์•„์ฃผ ๋ฐ”๋žŒ์งํ•˜๋ฉฐ, ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค.

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ