Deep_Dev
article thumbnail
Published 2023. 12. 28. 22:11
[iOS][SwiftUI] Init () ๐ŸŽ iOS/SwiftUI

๐Ÿ“š ์ดˆ๊ธฐํ™”์— ๋Œ€ํ•ด์„œ

AppDelegate()

init()

 

 

 

AppDelegate๋ฅผ ์ด์šฉํ•œ ์ดˆ๊ธฐํ™”

์ด ๋ฐฉ๋ฒ•์€ ์‚ฌ์‹ค AppDelegate์˜ ์—ญํ• (์ง„์ž…์ )์„ ํ•˜๋Š” ๊ฒƒ์ด ๋”ฐ๋กœ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ์žฅ๋˜์ง€๋Š” ์•Š๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

@main
struct PushMessageApp: App {
	var body: some Scene {
    	WindowGroup {
        	ContentView()
        }
    }
}

์œ„ ์ฝ”๋“œ์—์„œ ๋ณด์ด๋Š” App ํ”„๋กœํ† ์ฝœ ์ž์ฒด๊ฐ€ ์•ฑ์˜ ๊ตฌ์„ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ๊ตฌ์กฐ์ฒด์— ํ”„๋กœํ† ์ฝœ์„ ์ง€์ •ํ•ด์ฃผ์–ด์„œ ์•ฑ์˜ ์‹œ์ž‘(Presenting) ์ง€์ ์„ ์ •ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ @main์„ ํ†ตํ•˜์—ฌ ์•ฑ์ด ์‹œ์ž‘๋˜๊ณ  ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด๊ฐ€์•ผํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ช…์‹œํ•ด์ฃผ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— AppDelegate๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์•ฑ์˜ ์ดˆ๊ธฐํ™” ์ด์™ธ์—๋„ AppDelegate ํด๋ž˜์Šค์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฒƒ๋“ค์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„๋•Œ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

์ด๋• UIApplicationDelegate์™€ UIApplicationDelegateAdaptor๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž„์‹œ๋กœ AppDelegate๋ฅผ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

@main
strcut PushMessageApp: App {
	@UIApplicationDeleagteAdaptor(AppDelegate.self) var delegate
    
    let gcmMessageIDKey = "gcm.message_id" // FCM ํ†ต์‹ ์„ ์œ„ํ•ด ํ•„์š”ํ•œ Key
    var body: some Scene {
    	WindowGroup {
        	ContentView()
        }
    }
}

@UIApplicationDeleagteAdaptor๋ฅผ ํ†ตํ•ด UIKit๊ณผ ๋™์ผํ•˜๊ฒŒ delegate๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์„ค์ •ํ•ด์ค€๋‹ค. ์ด ๋ถ€๋ถ„์ด ์žˆ์–ด์•ผ delegate๋กœ ์œ„์ž„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

class AppDelegate: NSObject, UIApplicationDelegate {
 	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    // ์ดˆ๊ธฐํ™” ๋ถ€๋ถ„ (์•ฑ์ด ์‹คํ–‰๋˜๊ณ  ์ฒ˜์Œ ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ)
    }
    ...
}

AppDelegate๋ฅผ ์„ ์–ธํ•  ๋•Œ, NSObject, UIApplicationDelegate ๋‘ ํ”„๋กœํ† ์ฝœ์„ ์„ ์–ธํ•˜์—ฌ AppDelegate์˜ ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  ๋‚œ ๋’ค์— ๊ธฐ์กด UIKit์—์„œ ์‚ฌ์šฉํ•˜๋Š” AppDelegate์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ฒƒ์ด๋‹ค. ์ดˆ๊ธฐํ™” ๋ถ€๋ถ„์— Push ๊ด€๋ จ ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋“ฑ์„ ์„ ์–ธํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ํ‘ธ์‹œ ๊ด€๋ จ ์ฝ”๋“œ๋Š” ํŒŒ์ด์–ด๋ฒ ์ด์Šค์˜ ํด๋ผ์šฐ๋“œ ๋ฉ”์‹œ์ง•์„ ์‚ฌ์šฉํ–ˆ๊ธฐ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์ฝ”๋“œ๋ฅผ ๊ฒŒ์‹œํ•˜์ง€๋Š” ์•Š๊ฒ ๋‹ค.

 

Init()

SwiftUI๋Š” init()์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, AppDelegate๋ฅผ ๋งŒ๋“ค๋ฉด์„œ๊นŒ์ง€ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

@main
struct PushMessageApp: App {

    let gcmMessageIDKey = "gcm.message_id"
    
    init() {
   		// PushService ์‹ฑ๊ธ€ํ†ค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ด€๋ฆฌ
    	PushService.shared.initService()
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

์œ„ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•œ Push ๊ด€๋ จ ์ดˆ๊ธฐํ™”๋Š” ์‹ฑ๊ธ€ํ†ค์„ ์‚ฌ์šฉํ•˜์—ฌ init()์—์„œ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ์„ค์ •ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‚ฌ์‹ค ์• ํ”Œ์—์„œ ์›ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ตฌํ˜„ํ•œ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

 

์• ์ดˆ์— AppDelegate๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋ง๋ผ๊ณ  ์—†์•ด๋Š”๋ฐ, ๋‹ค์‹œ ๋งŒ๋“œ๋Š”๊ฒƒ ์ž์ฒด๊ฐ€ ๋ชจ์ˆœ๊ฐ™๋‹ค.

 

SwiftUI LifeCycle 

@main
struct PushMessageApp: App {
    @Environment(\.scenePhase) var scenePhase
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }.onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
            case .active:
                print("active State")
            case .background:
                print("backgound State")
            case .inactive:
                print("Inactive State")
            default:
                print("Default State")
            }
        }
    }
}

iOS 14๋ฒ„์ „๋ถ€ํ„ฐ ScenePhase ๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  SwiftUI์—์„œ๋Š” @Enviroment ์†์„ฑ ๋ž˜ํผ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  onChange() ์ˆ˜์ •์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ LifeCycle ์ƒํ™ฉ์ผ๋•Œ์˜ ์ž‘์—…์„ ์ •์˜ํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

 

'๐ŸŽ iOS > SwiftUI' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SwiftUI] Property Wrapper โ‘ก  (3) 2024.03.27
[SwiftUI] ํ”„๋กœํผํ‹ฐ๋ž˜ํผ ์ •๋ฆฌ  (0) 2024.03.17
[iOS][SwiftUI] @State @Binding  (1) 2024.01.07
[iOS][SwiftUI] Save Color in UserDefaults  (0) 2024.01.01
[iOS][SwiftUI] @main @UIApplicationMain  (0) 2023.12.28