我正在使用SwiftUI进行一个项目,并且在接收诸如应用程序进入前台和后台等事件时遇到麻烦。事件根本就没有发生。请参考下面的代码。
import SwiftUI
@main
struct testApp: App {
@Environment(.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
.onChange(of: scenePhase) { phase in
if phase == .background {
print("background")
}
if phase == .active {
print("active")
}
if phase == .inactive {
print("inactive")
}
}
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, world!").padding()
}
}
Xcode版本是12.5,在iOS 14.0 - 14.5模拟器上运行。我在iOS 15上试用了一下,效果很好。这是iOS 14的bug吗?
我是开发iPhone应用程序的新手。如有任何帮助,不胜感激。
你可以使用。onreceive()作为你需要的通知发布者。