๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] 2. Firebase ์‚ฌ์šฉํ•˜๊ธฐ(ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์•„์›ƒ)

ibelieveinme 2023. 6. 20. 23:38
728x90

https://firebase.google.com/docs/ios/setup?hl=ko&authuser=0 

 

Apple ํ”„๋กœ์ ํŠธ์— Firebase ์ถ”๊ฐ€  |  Apple ํ”Œ๋žซํผ์šฉ Firebase

Google I/O 2023์—์„œ Firebase์˜ ์ฃผ์š” ์†Œ์‹์„ ํ™•์ธํ•˜์„ธ์š”. ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ ์˜๊ฒฌ ๋ณด๋‚ด๊ธฐ Apple ํ”„๋กœ์ ํŠธ์— Firebase ์ถ”๊ฐ€ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜์„ธ์š”

firebase.google.com


1. ๋กœ๊ทธ์ธ(๋น„๋ฐ€๋ฒˆํ˜ธ ์ธ์ฆ) ๊ตฌํ˜„ํ•˜๊ธฐ

https://firebase.google.com/docs/auth/ios/password-auth?hl=ko&authuser=0 

 

Apple ํ”Œ๋žซํผ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ธฐ๋ฐ˜ ๊ณ„์ •์œผ๋กœ Firebase์— ์ธ์ฆ  |  Firebase ์ธ์ฆ

Google I/O 2023์—์„œ Firebase์˜ ์ฃผ์š” ์†Œ์‹์„ ํ™•์ธํ•˜์„ธ์š”. ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ ์˜๊ฒฌ ๋ณด๋‚ด๊ธฐ Apple ํ”Œ๋žซํผ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ธฐ๋ฐ˜ ๊ณ„์ •์œผ๋กœ Firebase์— ์ธ์ฆ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜

firebase.google.com

Firebase Document ๊ฐ€ ๋„ˆ๋ฌด ์ž˜๋˜์–ด ์žˆ์–ด์„œ ๋”ฐ๋ผํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

1) ์ธ์ฆ๊ณผ ๊ด€๋ จ๋œ firebase ๋ชจ๋“ˆ import ํ•˜๊ธฐ 

 

2) ์ฝ”๋“œ์™€ ์—ฐ๊ฒฐํ•˜๊ธฐ

 

๊ณ„์ • ์ƒ์„ฑ์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์— ์—ฐ๊ฒฐํ•ด์ค€๋‹ค. ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

import UIKit
import FirebaseAuth

class RegisterViewController: UIViewController {

    @IBOutlet weak var emailTextfield: UITextField!
    @IBOutlet weak var passwordTextfield: UITextField!
    
    @IBAction func registerPressed(_ sender: UIButton) {
        
        if let email = emailTextfield.text, let password = passwordTextfield.text {
            Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
                if let e = error {
                    print(e)
                }
                else {
                    self.performSegue(withIdentifier: "RegisterToChat", sender: self)
                }
            }
        }
    }
    
}

text field ๋กœ email ๊ณผ password ๋ฅผ๋ฐ›๊ณ , ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค. error ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์ƒˆ ํ™”๋ฉด์œผ๋กœ ์ „ํ™˜ํ•œ๋‹ค.

 

3) firebase ๊ถŒํ•œ์—์„œ Sign-in method ์— ์ด๋ฉ”์ผ/๋น„๋ฐ€๋ฒˆํ˜ธ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ

์ด๋ฉ”์ผ, ๋น„๋ฐ€๋ฒˆํ˜ธ ์‚ฌ์šฉํ•ด์„œ ๋กœ๊ทธ์ธ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„ํ•˜๊ฒ ๋‹ค๋Š” ๋ง!

 

4) ์‹คํ–‰

์•„์ด๋””/ํŒจ์Šค์›Œ๋“œ ์ž…๋ ฅํ›„ ๋“ฑ๋ก์„ ๋ˆ„๋ฅด๋ฉด Firebase ์— ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ 6๊ธ€์ž ์ด์ƒ ์‚ฌ์šฉํ•˜๋ผ๋Š” ์˜ค๋ฅ˜์™€ iCloud Keychain ์„ ์ด์šฉํ•ด์„œ ๋” ๊ฐ•๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“ค๋ผ๋Š” ๊ฒฝ๊ณ ๊ฐ€ ๋œฌ๋‹ค.

 

2. ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

Firebase Document์— ์žˆ๋Š” ๋Œ€๋กœ email ๊ณผ password ๋ฅผ ๋ฐ›์•„์„œ signIn ํ•จ์ˆ˜์— ์ „๋‹ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋ณธ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” login ์ฐฝ -> ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ ๋ฐ”๋กœ ์ฑ„ํŒ…์ฐฝ์œผ๋กœ ์ „ํ™˜๋˜๋„๋ก ํ–ˆ๋‹ค.

 

์ฐธ๊ณ ๋กœ [weak self] ๋ผ๋Š” ๊ตฌ๋ฌธ์ด ๋‚˜์™”๋Š”๋ฐ, ๋‚ด๊ฐ€ ์ดํ•ดํ•œ ๋ฐ”๋กœ๋Š” ๊ทธ๋ƒฅ self๋งŒ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ Swift ARC(Automatic reference counting) ์‹œ์Šคํ…œ์ด ์ž‘๋™์•ˆํ•˜๋Š” ์ƒํ™ฉ์ด ์ƒ๊ธธ ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜์—ฌ weak self๋กœ ๋ช…์‹œํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์•„๋ž˜ ๋งํฌ ์ฐธ์กฐ.

 

https://www.quora.com/What-does-weak-self-mean-in-a-Swift-Closure

 

What does “weak self” mean in a Swift Closure?

Answer: [code ][weak self][/code] is “syntactic sugar” built into the Swift language and basically means that the closure is getting a weak rather than a strong reference to [code ]self[/code]. So, this code: [code]let myClosure = { [weak self] in self

www.quora.com

 

์ „์ฒด์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

import UIKit
import FirebaseAuth

class LoginViewController: UIViewController {

    @IBOutlet weak var emailTextfield: UITextField!
    @IBOutlet weak var passwordTextfield: UITextField!

    @IBAction func loginPressed(_ sender: UIButton) {
        if let email = emailTextfield.text, let password = passwordTextfield.text{
            Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
                if let e = error {
                    print(e)
                }
                else{
                    self.performSegue(withIdentifier: "LoginToChat", sender: self)
                }
            }
        }
    }
}

๊ทผ๋ฐ Angela๋Š” ๊ทธ๋ƒฅ weak self ํ‚ค์›Œ๋“œ ๋ช…์‹œ ์—†์ด ์ผ๋‹ค. Swift ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜๋ฉด์„œ ARC๊ฐ€ ๋” ๋˜‘๋˜‘ํ•ด์ง„๊ฒŒ ์•„๋‹๊นŒ..?

 

๋ฌดํŠผ ์ž‘๋™ ๋ชจ์Šต์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

3. ๋กœ๊ทธ์•„์›ƒ ๊ตฌํ˜„ํ•˜๊ธฐ

1) Logout ๋ฒ„ํŠผ์œผ๋กœ ์‚ฌ์šฉํ•  bar button ์„ ๋งŒ๋“ค์–ด์„œ ์ฝ”๋“œ์— IBAction ํ•จ์ˆ˜๋กœ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

2) Firebase Document ์— ์žˆ๋Š” logout ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค.

3) logout ์‹œ ๋งจ ์ฒ˜์Œ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋„๋ก popToRootViewController ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค.

import UIKit
import FirebaseAuth

class ChatViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var messageTextfield: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func sendPressed(_ sender: UIButton) {
    }
    
    @IBAction func logOutPressed(_ sender: UIBarButtonItem) {
        do {
            try Auth.auth().signOut()
            navigationController?.popToRootViewController(animated: true)
            
        } catch let signOutError as NSError {
          print("Error signing out: %@", signOutError)
        }
    }
}

navigationcontroller ์˜ popToRootViewController ํ•จ์ˆ˜๋Š” ๋งจ ์ฒ˜์Œ Root ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

728x90