2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SwiftUI】SwiftUIでフォアグラウンド、バックグラウンドの検知

Posted at

何をやるのか?

今回はSwiftUIでフォアグラウンド、バックグラウンドの検知する方法を紹介してきたいと思います。
この記事を投稿しようと思った経緯としては、ViewModifierのonAppearではフォアグラウンド時の検知ができなかったため、備忘録として残しておこうといった感じです。

ScenePhaseでアプリの動作状態の検知

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

    var body: some View {
        Text("Hello, World!")
            .onChange(of: scenePhase) { newPhase in
                switch newPhase {
                    case .inactive:
                        print("フォアグラウンドだが、動作停止時")
                    case .active:
                        print("フォアグラウンド時")
                    case .background:
                        print("バックグラウンド時")
                }
            }
    }
}

参考文献

https://developer.apple.com/documentation/swiftui/scenephase
https://stackoverflow.com/questions/63423845/swiftui-onappear-only-running-once

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?