Live Activity isn’t working on the iOS 17.5 Simulator but works on a physical device. When I first close the app, it updates once, but then nothing updates anymore.
Supports Live Activities – YES
Signing & Capabilities
. Push notification
. Background modes
. Background fetch
. Remote notification
. Background processing
struct OrderLiveActivityView: View {
@State private var activity: Activity<OrderWidgetAttributes>? = nil
@State private var progress: CGFloat = 0.33
@State var activityTimer: Timer?
var body: some View {
VStack {
Button {
start()
} label: {
Text("Start")
.bold()
}
.padding()
Button {
removeLiveActivity()
} label: {
Text("Remove")
.bold()
}
.padding()
}
.padding()
}
@ViewBuilder
var progressBarView: some View {
VStack {
HStack {
ForEach(0..<4) { index in
Circle()
.fill(index <= Int(progress * 2) ? Color.green : Color.gray)
.frame(width: 20, height: 20)
.overlay(
Text("(index + 1)")
.foregroundColor(.white)
.font(.caption)
)
if index < 3 {
Spacer()
}
}
}
.padding(.top, 10)
.padding(.horizontal, 10)
ZStack {
// Points above the progress bar
HStack {
ForEach(0..<4) { index in
Circle()
.fill(Color.white)
.frame(width: 8, height: 8)
if index < 3 {
Spacer()
}
}
}
// Custom Progress Bar
ZStack {
Rectangle()
.fill(Color.white)
.frame(height: 2)
.cornerRadius(3)
}
}
.padding(.horizontal)
.padding(.bottom)
}
.background(Color.black.opacity(0.9))
.cornerRadius(8)
}
func startStatusUpdateTimer() {
activityTimer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: true) { _ in
updateOrderStatus()
}
}
func stopStatusUpdateTimer() {
// Invalidate the timer when no longer needed
activityTimer?.invalidate()
activityTimer = nil
}
func updateOrderStatus() {
Task {
if let activity = activity {
let currentStatus = activity.content.state.status
let newStatus = nextStatus(currentStatus)
let newContentState = OrderWidgetAttributes.ContentState(
status: newStatus,
estimatedDeliveryTime: activity.content.state.estimatedDeliveryTime
)
let updatedContent = ActivityContent(state: newContentState, staleDate: nil)
if #available(iOS 16.2, *) {
Task{
await activity.update(updatedContent, alertConfiguration: nil)
}
} else {
Task{
await activity.update(using: newContentState)
}
}
} else {
print("Not found activity)")
}
}
}
func nextStatus(_ currentStatus: OrderStatus) -> OrderStatus {
let statuses = OrderStatus.allCases
if let currentIndex = statuses.firstIndex(of: currentStatus) {
let nextIndex = (currentIndex + 1) % statuses.count
let status = statuses[nextIndex]
if status == .ready {
stopStatusUpdateTimer()
}
return status
}
return .ready
}
func start() {
guard activity == nil else {return}
let attr = OrderWidgetAttributes(orderID: UUID())
let now = Date()
// 10 saniye sonrası için tarihi hesapla
let tenSecondsLater = now.addingTimeInterval(10)
// ClosedRange<Date> oluştur
let dateRange: ClosedRange<Date> = now...tenSecondsLater
let state = OrderWidgetAttributes.ContentState(status: .inQueue, estimatedDeliveryTime: dateRange)
let content = ActivityContent(state: state, staleDate: nil)
do {
self.activity = try Activity<OrderWidgetAttributes>.request(attributes: attr, content: content, pushType: .token)
startStatusUpdateTimer()
print("ok")
} catch let error {
print(error.localizedDescription)
}
}
func removeLiveActivity() {
// Ensure the activity exists
guard let activity = activity else {
print("No activity to remove.")
return
}
Task {
await activity.end(activity.content, dismissalPolicy: .immediate)
}
}
}
#Preview {
OrderLiveActivityView()
}
I’ve done all the requirements, but Live Activity isn’t updating. What might I be missing?