์ƒˆ์†Œ์‹

๐Ÿ“ฑ iOS/-- UIKit

(iOS) ์•Œ๋ฆผ์ฐฝ, ์•ก์…˜ ์‹œํŠธ , ๋กœ์ปฌ ํ‘ธ์‹œ์•Œ๋žŒ (UIAlertController , UserNotification)

  • -

๋ฉ”์‹œ์ง€ ์ „๋‹ฌ

1. ๋ฉ”์‹œ์ง€ ์•Œ๋ฆผ์ฐฝ - UIAlertController

์•ฑ ๋‚ด์—์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ๊ฒฝ์šฐ

  1. ๋ฉ”์‹œ์ง€์ฐฝ (์•Œ๋ฆผ์ฐฝ)
    • ์•ฑ์ด ์‹คํ–‰ ์ค‘์ผ๋•Œ๋งŒ ๋™์ž‘ํ•œ๋‹ค. - ์ด๊ฒƒ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋กœ์ปฌ, ์„œ๋ฒ„ ์•Œ๋ฆผ์ด ์ƒ๊ฒผ๋‹ค.
  2. ๋กœ์ปฌ ์•Œ๋ฆผ (Local notification)
    • ์„œ๋ฒ„ ์•Œ๋ฆผ๊ณผ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ๋น„์Šทํ•˜์ง€๋งŒ, ์ถœ์ฒ˜๋Š” ๋‹ค๋ฅด๋‹ค.
    • ์ฒ˜์Œ์— ์•ฑ์„ ์ œ์ž‘ํ•  ๋•Œ ์•ฑ ๋‚ด๋ถ€์˜ ์ฝ”๋“œ์— ์˜ํ•ด ๋™์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋™์ ์œผ๋กœ ๋ฐ˜์‘ํ•˜์ง€ ๋ชปํ•œ๋‹ค.
  3. ์„œ๋ฒ„ ์•Œ๋ฆผ (Server notification)
    • ๋ณ„๋„์˜ ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
    • APNs(Apple Push Notification Service)๋ผ๋Š” ์• ํ”Œ์˜ ๋ฉ”์‹œ์ง•์‹œ์Šคํ…œ์—๊ฒŒ ๋ณด๋‚ธ ๋ฉ”์‹œ์ง€๊ฐ€ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
    • ๋ฐœ์†ก์„ ์œ„ํ•ด์„œ๋Š” ๋ณ„๋„์˜ ์ธ์ฆ์„œ ํ˜น์€ ์ธ์ฆํ† ํฐ์ด ํ•„์š”ํ•˜๋‹ค.
    • ์‚ฌ์šฉํ•˜๊ธฐ ๊นŒ๋‹ค๋กญ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์—… ์„œ๋น„์Šค์—์„œ ์ฃผ๋กœ ์ด์šฉ๋œ๋‹ค.

Provider : ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ์ž(๊ณต๊ธ‰์ž), ์ฃผ๋กœ 3rd Party Server์—์„œ ์ˆ˜ํ–‰๋œ๋‹ค.

iOS์—์„œ๋Š” ViewController์˜ ์ผ์ข…์ธ UIAlertController๋ฅผ ํ†ตํ•ด ๋ฉ”์‹œ์ง€์ฐฝ์„ ๊ตฌํ˜„ํ•œ๋‹ค. iOS 8.0 ์ดํ›„์— ๋“ฑ์žฅํ–ˆ๋‹ค. (UIAlertView๋Š” iOS 8.0 ๋ฒ„์ „ ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ๊ฐ์ฒด์ด๋ฏ€๋กœ ํ˜„์žฌ๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.)

โ€‹ <์™ผ์ชฝ์ด ์•ก์…˜์‹œํŠธ, ์˜ค๋ฅธ์ชฝ์ด ์•Œ๋ฆผ์ฐฝ>

๋ฉ”์‹œ์ง€์ฐฝ์˜ ์ข…๋ฅ˜

  1. ์•Œ๋ฆผ์ฐฝ (Alrert) - Modal ๋ฐฉ์‹
    • ๊ฒฝ๊ณ ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ
    • ์ทจ์†Œํ•  ๊ธฐํšŒ๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
    • ์ค‘์š” ์ •๋ณด ํ‘œ์‹œ
  2. ์•ก์…˜ ์‹œํŠธ (Action Sheet)
    • ์„ ํƒํ•ด์•ผํ•  ๋ชฉ๋ก์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ
    • ์ƒˆ๋กœ์šด ์ž‘์—… ์ฐฝ์„ ์—ฌ๋Š” ๊ฒฝ์šฐ

๋ชจ๋‹ฌ(Modal)

๊ฒฐ์ •์ ์ธ ์ฐจ์ด๋Š” ๋ชจ๋‹ฌ(Modal) ์—ฌ๋ถ€์ด๋‹ค.

๋ชจ๋‹ฌ์ด๋ž€ ์ฐฝ์ด ๋‹ซํžˆ๊ธฐ ์ „์—๋Š” ํ•ด๋‹น ์ฐฝ์˜ ์˜์—ญ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ๋ฐ˜์‘ํ•  ์ˆ˜ ์—†๋„๋ก ์ž ๊ธฐ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ฒŒ๋œ๋‹ค. ์•ก์…˜ ์‹œํŠธ ๊ฐ™์€๊ฒฝ์šฐ์—๋Š”, ์•ก์…˜์‹œํŠธ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๋ถ€๋ถ„์„ ํ„ฐ์น˜ํ•˜๊ฒŒ ๋˜๋ฉด ์•ก์‹  ์‹œํŠธ์ฐฝ์ด ๋‹ซํžˆ๊ฒŒ ๋œ๋‹ค.

๋น„๋™๊ธฐ (Asynchronize) ๋ฐฉ์‹

์•Œ๋ฆผ์ฐฝ ์ด์™ธ์˜ ๋ถ€๋ถ„์„ ํ„ฐ์น˜ํ•ด๋„ ๋ฐ˜์‘์ด ์—†๋‹ค๋Š” ๊ฒƒ์€, ์•ฑ์˜ ์‹คํ–‰์ด ์ค‘์ง€๋˜์—ˆ๋‹ค๋Š” ๋œป๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค. ์•Œ๋ฆผ์ฐฝ์€ ๋น„๋™๊ธฐ๋ฐฉ์‹์œผ๋กœ ์‹คํ–‰๋œ๋‹ค. ๋น„๋™๊ธฐ ๋ฐฉ์‹์€ ๊ธฐ์กด์˜ ์‹คํ–‰ ํ๋ฆ„์„ ๋ฐฉํ•ดํ•˜์ง€ ์•Š๋Š” ์„ ์—์„œ ์ƒˆ๋กœ์šด ์‹คํ–‰ํ๋ฆ„์„ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰, ์•Œ๋ฆผ์ฐฝ์ด ๋– ์žˆ์–ด๋„ ์•ฑ์˜ ์ฝ”๋“œ๋“ค์€ ๊ณ„์† ์‹คํ–‰๋˜๊ณ ์žˆ๋‹ค.

1 - (1) UIAlertController

๋ฉ”์‹œ์ง€ ์ฐฝ์€UIAlertController UIAlertAction ๋‘๊ฐ€์ง€๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. UIAlertController๊ฐ€ ์œˆ๋„์šฐ(๋ฉ”์‹œ์ง€ ์ฐฝ ์ž์ฒด)๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜๊ณ , UIAlertAction์€ ๋ฉ”์‹œ์ง€ ์ฐฝ ๋‚ด๋ถ€์— ๋“ค์–ด๊ฐˆ ๋ฒ„ํŠผ์„ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋œ๋‹ค.

let alert = UIAlertController(title: "Alert", message: "Sample", preferredStyle: UIAlertController.Style.alert) // UIAlertController์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๊ธฐ, preferredStyle์—์„œ Action sheet์ธ์ง€, alert์ธ์ง€ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

let cancel = UIAlertAction(title: "cancel", style: UIAlertAction.Style.cancel) // ๋ฒ„ํŠผ์ƒ์„ฑ 

alert.addAction(cancel) // ์•Œ๋ฆผ์ฐฝ์— ๋ฒ„ํŠผ์ถ”๊ฐ€
self.present(alert, animated: false) // ์•Œ๋ฆผ๋„์šฐ๊ธฐ , ์ž๋™์œผ๋กœ ๋‹ซํžˆ๋ฏ€๋กœ dismiss๋Š” ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

UIAlertAction.Style

์•Œ๋ฆผ์ฐฝ ๋ฒ„ํŠผ์˜ ์Šคํƒ€์ผ์ด๋‹ค. ์ด 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

UIAlertAction์˜ ํŒŒ๋ผ๋ฏธํ„ฐ handler

๋ฒ„ํŠผ์ด ํด๋ฆญ ๋˜์—ˆ์„๋•Œ ์‹คํ–‰ํ•  ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•œ๋‹ค. ํ•จ์ˆ˜๋‚˜ ํด๋กœ์ €๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ์ด๋•Œ ๋ฒ„ํŠผ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ์ •๋ณด๋„ ์ธ์ž๊ฐ’์œผ๋กœ ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” UIAlertActionํƒ€์ž…์˜ ์ธ์ž๊ฐ’ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ํ˜•์‹์œผ๋กœ ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์–ธ๋”๋ฐ”๋กœ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.

UiAlertAction(title: "cancel", style: .cancel) {(_) in 
// code
// code                                       
}

์—ด๊ฑฐํ˜• ์ถ•์•ฝ

์—ด๊ฑฐํ˜•์˜ ๊ฒฝ์šฐ๋Š” ์ถ•์•ฝํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

preferredStyle: UIAlertcontroller.Style.alert ๋ถ€๋ถ„์„ .alert๋กœ ์ ์–ด๋„ ์ปดํŒก์ผ๋Ÿฌ๋Š” ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ž…๋ ฅ ํ•„๋“œ๋ฅผ ๊ฐ€์ง„ ๋ฉ”์‹œ์ง€์ฐฝ

๋ฉ”์‹œ์ง€ ์ฐฝ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๋‹ค. (Appstore login์ฐฝ์ด ๋Œ€ํ‘œ์ ์ด๋‹ค) ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์€UIAlertController ๊ฐ์ฒด์— ํ…์ŠคํŠธ ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. <alert ์ธ์Šคํ„ด์Šค>.addTextField(configurationHadnelr:)๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

...
...// alert ์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์•Œ๋ฆผ์ฐฝ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ (์œ— ๋ถ€๋ถ„ ์ฝ”๋“œ์™€ ๊ฐ™์œผ๋ฏ€๋กœ ์ƒ๋žต)

alert.addTextField(configurationHandler: {(textField) in 
    textField.placeholder = "Password" // ํ”Œ๋ ˆ์ด์Šค ํ™€๋”
    textField.isSecureTextEntry = true // ์•”ํ˜ธ์ฒ˜๋ฆฌ
})
self.present(alert, animated:false)
  • ๋ฉ”์‹œ์ง€์ฐฝ์˜ ํ…์ŠคํŠธ ํ•„๋“œ๋Š” .textField์†์„ฑ์„ ํ†ตํ•ด ์ฐธ์กฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • textField๋Š” ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋ฐฐ์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. (์ฒซ๋ฒˆ์งธ ํ…์ŠคํŠธ ํ•„๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋ ค๋ฉด ์ธ๋ฑ์Šค 0๋ฒˆ)
  • ํ…์ŠคํŠธ ํ•„๋“œ์˜ ์†์„ฑ์— ์ถ”๊ฐ€ํ•  ์„ค์ •์ด ์—†๋‹ค๋ฉด, alert.addTextField()๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

Local ์•Œ๋ฆผ

๋กœ์ปฌ ์•Œ๋ฆผ์€ ์•ฑ ๋‚ด๋ถ€์˜ ๋ฉ”์‹œ์ง€๋ฅผ iOS ์•Œ๋ฆผ ์„ผํ„ฐ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์•ฑ์ด ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜, ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์ผ ๋•Œ๋„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋กœ์ปฌ ์•Œ๋ฆผ์€ iOS์Šค์ผ€์ค„๋Ÿฌ์— ์˜ํ•ด ๋ฐœ์†ก๋˜๋Š”๋ฐ, ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ๊ณผ ๋ฐœ์†ก ์‹œ๊ฐ„์„ iOS์Šค์ผ€์ค„๋Ÿฌ์— ๋“ฑ๋กํ•ด๋†“์œผ๋ฉด ํ•ด๋‹น ์‹œ๊ฐ์— ๋งž์ถ”์–ด ๋ฐœ์†ก๋œ๋‹ค. ์•ฑ์ด ์‹คํ–‰์ค‘์ด๋ผ๋ฉด, ์•Œ๋ฆผ์ฐฝ์„ ์ด์šฉํ•˜๋Š”๊ฒŒ ํšจ์œจ์ ์ด๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋กœ์ปฌ์•Œ๋ฆผ์„ ์ด์šฉํ•˜๋Š”๊ฒŒ ํšจ์œจ์ ์ด๋‹ค.

iOS 10 ์ด์ „์—๋Š” UILocalNotification๊ฐ์ฒด ์—์„œ ๋กœ์ปฌ ์•Œ๋ฆผ์„ ์ฒ˜๋ฆฌํ–ˆ์ง€๋งŒ, ์ตœ๊ทผ์—๋Š” UserNotification ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

UserNotification ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ด์šฉํ•œ ๋กœ์ปฌ ์•Œ๋ฆผ

UserNotification์€ ์•Œ๋žŒ์„ ์œ„ํ•œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฐ์ฒด๋“ค์„ ํฌํ•จํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค. ์—ฌ๊ธฐ์— ์†ํ•œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์•Œ๋ฆผ์ฒ˜๋ฆฌ์™€ ๊ด€๋ จ๋œ ๊ฐ์ฒด๋“ค์€ UN์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ ‘๋‘์–ด๋ฅผ ๊ฐ€์ง„๋‹ค. ํ”„๋ ˆ์ž„์›Œํฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” import UserNotification์„ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

UserNotification์˜ ์ฃผ์š”๊ฐ์ฒด๋“ค

  1. UNMutableNotificationContent
    • ์•Œ๋ฆผ์— ํ•„์š”ํ•œ ๋ฉ”์‹œ์ง€์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ์†์„ฑ๋“ค์„ ๋‹ด๋Š” ์ฝ˜ํ…์ธ ์—ญํ• ์„ ํ•œ๋‹ค.
    • ์•ฑ ์•„์ด์ฝ˜์˜ ๋ฐฐ์ง€, ์‚ฌ์šด๋“œ ์„ค์ •๋„ ์ด ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ ํ•œ๋‹ค.
  2. UNTimeIntervalNotificationTrigger
    • ์•Œ๋ฆผ์˜ ๋ฐœ์†ก ์กฐ๊ฑด์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค.
    • ์•Œ๋ฆผ ๋ฐœ์†ก ์‹œ๊ฐ„, ๋ฐ˜๋ณต ์—ฌ๋ถ€ ๋“ฑ์„ Date()ํƒ€์ž… ํ˜น์€ UNTimeIntervalNotificationTrigger์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  3. UNNotificationRequest
    • 1, 2๋ฒˆ์˜ ๊ฐ์ฒด๋“ค์„ ํ†ตํ•ด์„œ ์•Œ๋ฆผ์— ํ•„์š”ํ•œ ์†์„ฑ๋“ค์„ ๊ฐ–์ถ”๊ณ , 1,2๋ฒˆ์„ ์ธ์ž๊ฐ’์œผ๋กœ ๋ฐ›์•„ ์ด ํด๋ž˜์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋กœ ์•Œ๋ฆผ ์š”์ฒญ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
  4. UNUserNotificationCenter
    • ์‹ค์ œ ๋ฐœ์†ก์„ ๋‹ด๋‹นํ•˜๋Š” ์•Œ๋ฆผ์„ผํ„ฐ์ด๋‹ค.
    • ์ด ๊ฐ์ฒด๋Š” ์‹ฑ๊ธ€ํ†ค ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•˜๋ฏ€๋กœ, ์ธ์Šคํ„ด์Šค๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.
    • current()๋ฅผ ํ†ตํ•ด์„œ ์ฐธ์กฐ ์ •๋ณด๋งŒ ๊ฐ€์ ธ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

์‹ค์Šต(์ฝ”๋“œ)

์•ฑ์„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์˜ฎ๊ฒผ์„๋•Œ ๋กœ์ปฌ ์•Œ๋ฆผ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ด๋‹ค. ๋กœ์ปฌ์•Œ๋ฆผ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค์‹œํ•œ๋ฒˆ ์ˆœ์„œ๋Œ€๋กœ ๊ธฐ์–ตํ•ด๋ณด๋ฉด,

  1. ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์•Œ๋ฆผ ํ—ˆ์šฉ์„ ์Šน๋ฝ๋ฐ›๊ธฐ.
  2. UIMutableNotificationContentํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•Œ๋ฆผ ์ฝ˜ํ…์ธ  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  3. ์•ฑ ์•„์ด์ฝ˜ badge๋ฅผ 1๋กœ ์„ค์ •
  4. TItle, subtitle ์ž…๋ ฅ
  5. ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์„ค์ •
  6. ์‚ฌ์šด๋“œ ์„ค์ • (UNNotificationSound)
  7. UNTimeIntervalNotificationTrigger ํด๋ž˜์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ, ์•Œ๋ฆผ ๋ฐœ์†ก ์กฐ๊ฑด์„ ์ง€์ •ํ•œ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ. (์‹œ๊ฐ„์„ค์ •๊ณผ ๋ฐ˜๋ณต์—ฌ๋ถ€๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.)
  8. UNNotificationRequestํด๋ž˜์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์•Œ๋ฆผ ์š”์ฒญ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ. ๊ฐ์ฒด ์•„์ด๋””๋„ ์„ค์ •ํ•œ๋‹ค. ๊ฐ์ฒด ์•„์ด๋””๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋Š ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€๋ฅผ ํด๋ฆญํ–ˆ๋Š”์ง€ ์‹๋ณ„ํ• ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋‚˜๋จธ์ง€ ์ธ์ž๊ฐ’์œผ๋กœ ์•Œ๋ฆผ ์ฝ˜ํ…์ธ ์™€ ์•Œ๋ฆผ ๋ฐœ์†ก ์กฐ๊ฑด ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.
  9. ์ƒ์„ฑ๋œ ์•Œ๋ฆผ ์š”์ฒญ ๊ฐ์ฒด๋ฅผ ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ ์„ผํ„ฐ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ ์„ผํ„ฐ๋Š” iOS์˜ ์Šค์ผ€์ค„๋ง ์„ผํ„ฐ์— ์ด ๊ฐ’์„ ๋“ฑ๋กํ•˜๊ณ , ๋ฐœ์†กํ•˜๊ฒŒ ๋œ๋‹ค.

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋กœ์ปฌ ์•Œ๋ฆผ ์Šน์ธ๋ฐ›๊ธฐ

AppDelegate.swift ํŒŒ์ผ

import UIKit
import UserNotifications
import CoreData

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if #available(iOS 10.0, *){ // iOS10.0 ์ด์ƒ๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
          //๊ฒฝ๊ณ ์ฐฝ, ๋ฐฐ์ง€, ์‚ฌ์šด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๋ฆผ์„ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ๋™์˜ ์—ฌ๋ถ€๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
            let notiCenter = UNUserNotificationCenter.current() 
            notiCenter.requestAuthorization(options: [.alert, .badge, .sound]){(didAllow, e) in}}else{
            }
        return true
    }
  ...
  ...
  ...

if #available()๊ตฌ๋ฌธ์€ iOS๋ฒ„์ „ ๋ณ„๋กœ ์‹คํ–‰ ๋กœ์ง์„ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•จ์ด๋‹ค.

requestAuthorization()๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ฆผ ์„ค์ •์— ๋Œ€ํ•œ ๋™์˜๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. options์ธ์ž๋Š” ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€๋ฅผ ๊ตฌ์„ฑํ•  ์š”์†Œ๋“ค์ด๋‹ค.(์•Œ๋ฆผ์ฐฝ, ๋ฐฐ์ง€, ์‚ฌ์šด๋“œ)

๋‘๋ฒˆ์งธ ์ธ์ž๋Š” ํด๋กœ์ €์ด๋‹ค. ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ didAllow๋Š” ์‚ฌ์šฉ์ž์˜ ๋™์˜ ์—ฌ๋ถ€๊ฐ€ BOOL๊ฐ’์œผ๋กœ ๋“ค์–ด์˜ค๊ณ , ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ e๋Š” ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ์— ์‚ฌ์šฉ๋˜๋Š” ์˜ค๋ฅ˜ ๊ฐ์ฒด ํƒ€์ž…์ด๋‹ค.

sceneWillResignActive(_:)

์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ๋“ค์–ด๊ฐ”์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. iOS13 ์ด์ „์—๋Š” AppDelegate์— applicationWillResignActive(_:)์— ์ •์˜๋˜์–ด ์žˆ์—ˆ์œผ๋‚˜, iOS13 ์ดํ›„ ์ƒ๋ช…์ฃผ๊ธฐ ์ด๋ฒคํŠธ ์ „๋‹ฌ๋ฐฉ์‹์ด ๋ณ€๊ฒฝ๋˜์–ด์„œ SceneDelegate์˜ sceneWillResignActive(_:)๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

SceneDeleagte.swift ํŒŒ์ผ

   func sceneWillResignActive(_ scene: UIScene) {
        if #available(iOS 13.0, *){
            UNUserNotificationCenter.current().getNotificationSettings{
                settings in if settings.authorizationStatus == UNAuthorizationStatus.authorized{
                    let nContent = UNMutableNotificationContent()
                  //์ฝ˜ํ…์ธ  ์†์„ฑ ์ •์˜
                    nContent.badge = 1
                    nContent.title = "๋กœ์ปฌ ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€"
                    nContent.subtitle = "๋‹ค์‹œ ์•ฑ์„ ์—ด์–ด์ฃผ์„ธ์š”"
                    nContent.body = "์™œ ์•ฑ์„ ๋‚˜๊ฐ€์…จ๋‚˜์š”?!"
                    nContent.sound = UNNotificationSound.default
                    nContent.userInfo = ["name" :
                    "Lee"]
                  //์•Œ๋ฆผ ๋ฐœ์†ก ์กฐ๊ฑด ๊ฐ์ฒด(์ฆ‰, ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ๋Œ์•„๊ฐ€๊ณ  5์ดˆ๋’ค์— ์•Œ๋ฆผ๋ฐœ์ƒ)
                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                    let request = UNNotificationRequest(identifier: "wakeup", content: nContent, trigger: trigger)                  //์•Œ๋ฆผ ์š”์ฒญ ๊ฐ์ฒด

                    UNUserNotificationCenter.current().add(request)                  //Notification Center์— ์ถ”๊ฐ€


                }else{
                    print("์‚ฌ์šฉ์ž๊ฐ€ ๋™์˜ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
                }
            }
        }else{
            //iOS 13 ์ดํ•˜ ๋ฒ„์ „
        }
    }

๊ฒฐ๊ณผํ™”๋ฉด(๋กœ์ปฌ์•Œ๋ฆผ)

๋ฐ›์€์•Œ๋ฆผ ์ฒ˜๋ฆฌํ•˜๊ธฐ

์‚ฌ์šฉ์ž๊ฐ€ ์•Œ๋ฆผ์„ ๋ฐ›๊ณ , ์ด๋ฅผ ํ„ฐ์น˜ํ–ˆ์„๋•Œ ๋ณ„๋„์˜ ์•ก์…˜์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. UserNotificationํ”„๋ ˆ์ž„์›Œํฌ์—์„œ๋Š” ์ด๋ฅผ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•œ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ด๋™ํ•˜๊ณ  5์ดˆ๋’ค์— ์•Œ๋žŒ์„ ์šธ๋ฆฌ๋„๋ก ์ฝ”๋“œ๋ฅผ ์งฐ๋‹ค. ์ด ์•Œ๋žŒ ๋ฉ”์‹œ์ง€๋ฅผ ํด๋ฆญํ–ˆ์„๋•Œ ๊ฐ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€

AppDelegate ํด๋ž˜์Šค ์ •์˜๋ถ€๋ถ„์— UNUserNotificationCenterDelegateํ”„๋กœํ† ์ฝœ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

import UIKit
import UserNotifications
import CoreData
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

AppDelegate ๋‚ด๋ถ€์˜ didFinishLaunchingWithOptions ํ•จ์ˆ˜์—์„œ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์ง€์ •ํ•ด์ค€๋‹ค.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if #available(iOS 10.0, *){
            let notiCenter = UNUserNotificationCenter.current()
            notiCenter.requestAuthorization(options: [.alert, .badge, .sound]){(didAllow, e) in}
            notiCenter.delegate = self
        }else{
            }
        return true
    }

๋‹ค์Œ์œผ๋กœ๋Š” AppDelegate.swift์— ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

  1. ์•ฑ์ด ์‹คํ–‰์ค‘์ด๋ฉด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ๋ฆผ์„ ํ„ฐ์น˜ํ–ˆ์„๋•Œ AppDelegate์— ๋‹ค์Œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๋ฉด ๋œ๋‹ค.

Willpresent ๋ฉ”์†Œ๋“œ

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("willpresent")
        if notification.request.identifier == "wakeup"{
            let userInfo = notification.request.content.userInfo
            print(userInfo["name"]!)
        }
        completionHandler([.alert, .badge,.sound])
    }
  1. ์•ฑ์€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์žˆ๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ๋ฆผ์„ ํ„ฐ์น˜ํ–ˆ์„๋•Œ

DidReceive ๋ฉ”์†Œ๋“œ

   @available(iOS 10.0 , *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.notification.request.identifier == "wakeup" {
            let userInfo = response.notification.request.content.userInfo
            print("didReceive")
            print(userInfo["name"]!)
        }
        completionHandler()
    }
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.