๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
iOS

[iOS] Notification Center ๐Ÿ“ก

by yangsubinn 2022. 3. 14.

Notification Center์ด๋ž€?

ํŠน์ • ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ,

ํ•ด๋‹น ์ด๋ฒคํŠธ์˜ ๋ฐœ์ƒ ์—ฌ๋ถ€๋ฅผ observe ํ•˜๊ฒ ๋‹ค๊ณ  ๋“ฑ๋กํ•œ ๋ชจ๋“  observer์—๊ฒŒ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฐ์ฒด

Notification Center

์–ธ์ œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„๊นŒโ“

  • ์ผ๋Œ€๋‹ค ๋˜๋Š” ๋‹ค๋Œ€๋‹ค ํ†ต์‹ ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
  • ์•ฑ ๋‚ด์—์„œ ๋ช…์‹œ์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ์ปดํฌ๋„ŒํŠธ ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ

  • ํผ์ด๋ผ๋Š” ์ด๋ฆ„์˜ notification์„ ๊ด€์ฐฐํ•˜๊ณ  ์žˆ๋Š” FirstVC์—๊ฒŒ ํ•ด๋‹น noti๊ฐ€ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค!
  • SecondVC์—์„œ dismiss completion์— ํ•ด๋‹น ํผ์ด๋ผ๋Š” ์ด๋ฆ„์˜ notification ์„ post ํ•ด์ฃผ๋ฉด
  • FirstVC์—์„œ ํผ์ด๋ผ๋Š” ์ด๋ฆ„์˜ notification ์„ ๊ด€์ฐฐํ•˜๊ฒ ๋‹ค๊ณ  addObserve ํ•˜๊ณ 
  • ex) ํ™”๋ฉด ์ „ํ™˜ ํ›„ pop ๋˜๋Š” dismissํ•  ๋•Œ ๋Œ์•„์˜จ VC์— ์†ํ•œ ๋ฌด์–ธ๊ฐ€๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ!

Notification Center ์‚ฌ์šฉ๋ฒ•

1๏ธโƒฃ addObserver

// FirstVC
NotificationCenter.default.addObserver(self, selector: #selector(setToastMessage), name: Notification.Name("showToast"), object: nil)

showToast ๋ผ๋Š” ์ด๋ฆ„์˜ Notification์„ ๊ด€์ฐฐํ•  observer๋Š” self์ด๊ณ ,

noti๊ฐ€ ๊ด€์ฐฐ๋˜๋ฉด setToastMessage๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒ ๋‹ค.

 

2๏ธโƒฃ post

// SecondVC
NotificationCenter.default.post(name: Notification.Name("showToast"), object: nil)

showToast๋ผ๋Š” ์ด๋ฆ„์˜ Notification์„ ๋ณด๋‚ด๊ฒ ๋‹ค!


Notification Center๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๊ธฐ

์ด ๊ธ€์„ ์“ฐ๊ฒŒ๋œ ๊ฐ€์žฅ ํฐ ์ด์œ ์ธ๋ฐ ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•ด์„œ ๊ทธ๋ƒฅ ์•ž์— ํ•œ๋‘๋งˆ๋”” ๋” ๋ถ™์—ฌ๋ดค์Šต๋‹ˆ๋‹ค..

๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค!

1๏ธโƒฃ object

post๋ฅผ ๋ณด๋‚ผ๋•Œ ์•ž์—์„œ nil๋กœ ์ฒ˜๋ฆฌํ•ด์คฌ๋˜ object๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

// SecondVC
NotificationCenter.default.post(name: Notification.Name("showToast"), object: "์•ˆ๋…•๐Ÿ˜Ž")

postํ•  ๋•Œ ๋ณด๋‚ด๊ณ ์ž ํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ..

// FirstVC
NotificationCenter.default.addObserver(self, selector: #selector(setToastMessage), name: Notification.Name("showToast"), object: nil)

// ... 
@objc
private func setToastMessage(_ notification: Notification) {
		...
    if let value = notification.object as? String {
        print(value) // ์•ˆ๋…•๐Ÿ˜Ž
    }
}

notification.object ๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

2๏ธโƒฃ userInfo

post๋ฅผ ๋ณด๋‚ผ๋•Œ userInfo์— [AnyHashable : Any] ํ˜•ํƒœ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

์—ฌ๋Ÿฌ key๊ฐ’์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹น

// SecondVC
NotificationCenter.default.post(name: Notification.Name("showToast"), 
		object: "nil", 
		userInfo: ["userName": "๋น„๋ฐ€์ž…๋‹ˆ๋‹ค", "hungry": true])

[”notificatonKey": value] ํ˜•ํƒœ๋กœ ๋ณด๋‚ด๊ณ ์ž ํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ ...

// FirstVC
NotificationCenter.default.addObserver(self, selector: #selector(setToastMessage), name: Notification.Name("showToast"), object: nil)

// ... 
@objc
private func setToastMessage(_ notification: Notification) {
		...
		guard var userName: String = notification.userInfo?["userName"] as? String else { return }
		guard let hungry: Bool = notification.userInfo?["hungry"] as? Bool else { return }
		print(userName) // ๋น„๋ฐ€์ž…๋‹ˆ๋‹ค
		print(hungry) // true
}

notification.userInfo[”notificatonKey”] ์„ ํ†ตํ•ด ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

Notification Center๋ฅผ ๋” ์•ผ๋ฌด์ง€๊ฒŒ ์“ฐ๋Š” ๋ฒ• ๐Ÿ˜Ž

extension ๋ถ„๋ฆฌ

extension Notification.Name {
    static let report = Notification.Name("report")
    static let showToast = Notification.Name("showToast")
}

์œ„์™€ ๊ฐ™์ด ๋”ฐ๋กœ ๋นผ์„œ ์ƒ์ˆ˜๋กœ ์ €์žฅํ•˜๋ฉด ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๊ณ  ํ‚ค๊ฐ’์— ์˜คํƒ€๋ฅผ ๋งŒ๋“œ๋Š” ๋“ฑ์˜ ์—๋Ÿฌ๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.. ๐Ÿ˜…

 


 

๊ฐ„๋‹จํ•˜๊ฒŒ ํ…Œ์ŠคํŠธํ•ด๋ดค๋˜ ์ฝ”๋“œ.. ๐Ÿ’ญ

 

GitHub - yangsubinn/Test-iOS: ์ด๊ฒƒ ์ €๊ฒƒ ๋‹ค ํ…Œ์ŠคํŠธํ•ด๋ณด๋Š” ๋ ˆํฌ๐Ÿ•Š

์ด๊ฒƒ ์ €๊ฒƒ ๋‹ค ํ…Œ์ŠคํŠธํ•ด๋ณด๋Š” ๋ ˆํฌ๐Ÿ•Š. Contribute to yangsubinn/Test-iOS development by creating an account on GitHub.

github.com