在做Apple Watch開發(fā)時候,有用到監(jiān)聽APP處于后臺以及進(jìn)入前臺的狀態(tài),按照以下代碼處理即可
@Environment(\.presentationMode) var presentationMode
Text("hello")
.onChange(of: scenePhase) { newValue in
switch newValue {
case .background:
print("程序進(jìn)后臺了")
saveStartTime()
case .active:
print("程序激活了")
case .inactive:
print("程序掛起來了。。。")
fetchStartTime()
default:
print("......")
}
}