SwiftUI iOS17/iPadOS17 scenePhaseのonChange変更

iOS16まで

import SwiftUI

struct MainView: View {
    @Environment(\.scenePhase) private var scenePhase

    var body: some View {
        VStack {
            Text("Hello, World!")
        }
        .onChange(of: scenePhase, perform: { value in
            switch(value) {
            case .active:
                print("active")
            case .background:
                print("background")
            case .inactive:
                print("inactive")
            @unknown default:
                print("default")
            }
        })
    }
}

iOS17から

import SwiftUI

struct MainView: View {
    @Environment(\.scenePhase) private var scenePhase

    var body: some View {
        VStack {
            Text("Hello, World!")
        }
        .onChange(of: scenePhase) { oldScenePhase, newScenePhase in
            switch(newScenePhase) {
            case .active:
                print("active")
            case .background:
                print("background")
            case .inactive:
                print("inactive")
            @unknown default:
                print("default")
            }
        }
    }
}

SwiftSwift,SwiftUI

Posted by shi-n