Hi, I'm trying to implement the iOS18 ControlWidget features. When i turn on a ControlWidgetToggle that action with a LiveActivityIntent, the toggle will turn off automatic. The toggle state is read from my sharemanger, but it state won't refresh even i use the ControlCenter.shared.reloadAllControls(). Here is my code.
struct TimerLiveActivityControl: ControlWidget {
var body: some ControlWidgetConfiguration {
StaticControlConfiguration(kind: Self.kind) {
ControlWidgetToggle(isOn: ShareManager.shared.isLiveActivityTimerOn, action: LiveActivityTimerIntent()) { isTurnedOn in
Image(systemName: isTurnedOn ? "fan.fill":"fan")
Text(isTurnedOn ? "Turned off" : "Turned On")
} label: {
Text("Live Activity Timer")
}
}
}
}
struct LiveActivityTimerIntent: LiveActivityIntent, SetValueIntent {
static var title: LocalizedStringResource { "Live Activity timer" }
@Parameter(title: "is Turned On")
var value: Bool
@MainActor
func perform() async throws -> some IntentResult {
TimerLiveActivityTimer.shared.duration = 10
TimerLiveActivityTimer.shared.startTimer()
return .result()
}
}
func startTimer() {
self.startActivity()
self.isLiveActivityTimerOn = true
Timer.scheduledTimer(withTimeInterval: TimeInterval(self.duration), repeats: false) { timer in
self.isLiveActivityTimerOn = false
ControlCenter.shared.reloadAllControls()
self.endActivity()
}
//The ControlWidgetToggle can't refresh even i refresh it all time
Timer.scheduledTimer(withTimeInterval: TimeInterval(0.1), repeats: true) { timer in
ControlCenter.shared.reloadAllControls()
}
}
However the code can work if I use a single SetValueIntent. My Xcode version is 16.0 beta, use iOS 18.0 simulator.