How can I re-create a CLKRelativeDateTextProvider in a WidgetKit complication?

I'm in the process of migrating one of my app's Apple Watch complications from ClockKit to WidgetKit.

In my ClockKit complications, there are some cases where a show the relative time since the last event in the app (for example, "25MIN").

With ClockKit, this was quite straightforward using a CLKTextProvider designed for this exact purpose:

CLKRelativeDateTextProvider(date: date, style: .offsetShort, units: [.hour, .minute])

This has always worked great, since it lets me specify that hours and minutes should be used, but not seconds, since they're not relevant here.

How can I accomplish the same thing with WidgetKit and SwiftUI?

The closest thing I've found is this:

Text(date, style: .relative)

It shows, for example: 14MIN 8SEC

This is not what I want, since it shows seconds. It makes the complication look messy with all the extra information, and is distracting because it updates every second until an hour has passed.

I suppose I could write my own logic to create a new timeline entry every minute to show the number of hours and minutes since the last update, but since CLKTextProvider works so nicely I wanted to check whether there is some way to accomplish the same thing without creating a new entry for every minute.

There seem to be some more customizable options with date formatters, but I haven't found a way to combine this with the SwiftUI version that updates automatically on the Apple Watch face: https://developer.apple.com/documentation/foundation/date/relativeformatstyle/3766585-presentation

  • I wish this was possible but fairly sure it's not. Also their relative dates that are available don't update in the WidgetLabel areas... Frustrating, yet another regression from WidgetKit

  • Oh no... I didn't even get that far yet, but I did want to show it in a .widgetLabel in one of my apps. It's interesting you gave the Heart Rate complication example, because that's exactly the one I was looking at and trying to replicate.

Add a Comment

Replies

I don't think Apple have bothered with allowing us to format these things. (Too hard?) I asked a similar thing three years ago and got no answer.

  • Yeah, I've tried a lot of different things trying to replicate this. As annoying as some of the ClockKit stuff was, CLKRelativeDateTextProvider was great. The closest I got was some custom formatters and using a TimelineView to update the Text every second, but then that didn't work in the context of a complication (widget).

Add a Comment

We do not have an exact API for that today but you are welcome to file an enhancement request at https://developer.apple.com/bug-reporting.