6

I have a widget with a SwiftUI Text timer date. It countdowns as expected. However, when the device is locked, the timer is frozen.

To reproduce it, add the SwiftUI Text(_:style:) view to a widget and place the widget on the "Today View". The countdown should work as expected. However, lock the phone then view the Today View in a locked state. The timer is frozen.

Below is the full working sample code:

import WidgetKit
import SwiftUI

struct Provider: TimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date())
    }

    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date())
        completion(entry)
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []

        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate)
            entries.append(entry)
        }

        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
}

struct TestWidget123EntryView : View {
    var entry: Provider.Entry

    var body: some View {
        Text(entry.date, style: .timer)
            .multilineTextAlignment(.center)
            .padding()
    }
}

@main
struct TestWidget123: Widget {
    let kind: String = "TestWidget123"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            TestWidget123EntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

struct TestWidget123_Previews: PreviewProvider {
    static var previews: some View {
        TestWidget123EntryView(entry: SimpleEntry(date: Date()))
            .previewContext(WidgetPreviewContext(family: .systemSmall))
    }
}

I don't have any data protected data and I tried using a timer but didn't work at all. How can I allow the Text timer to countdown in a locked state?

TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • That is UI only feature, but you'd rather need to do that manually via timeline to control from model layer. – Asperi Apr 17 '22 at 15:24
  • The thing is I already have 30 items scheduled a day for a week. To add one minute entries in the timeline is not feasible especially if the timer was designed to avoid this. I'm hoping I'm missing something. – TruMan1 Apr 18 '22 at 09:46
  • Also the relative time complication on the watch works in always-on display. How could this be that a small watch is able to do this but not an iPhone if its about performance. – TruMan1 Apr 18 '22 at 09:49
  • I am confident that `Text(entry.date, style: .timer)` should update continuously, locked or not. However I can confirm that the timer freezes with a bogus time when locked — but strangely, this only happens to one of my apps. My other app that uses `Text(…, style: .timer)` continues to update just fine. I don’t see any reason why these apps would behave differently… Maybe because one uses in-app intent handling and the other has an intent extension? Or this is an issue with a specific version of Xcode? – Adam May 27 '22 at 18:09
  • I think its a bug in iOS 16 because my app works fine on iOS 15 but just shows a static value on iOS 16. – Ric Santos Sep 08 '22 at 11:26

0 Answers0