1

Currently, my code only updates when first launching (in this case, booting Apple Watch). I want the current time to automatically update every minute, so it could be read as h:mm a.

if complication.family == .utilitarianLarge {

        let template = CLKComplicationTemplateUtilitarianLargeFlat()

        let currentDateTime = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "h:mm a"
        let dateTimeString = formatter.string(from: currentDateTime)
        let timeLineText = dateTimeString

        template.textProvider = CLKSimpleTextProvider(text: "\(timeLineText)")

        let timelineEntry = CLKComplicationTimelineEntry(date: currentDateTime, complicationTemplate: template)
        handler(timelineEntry)
    }

For more context, this is a complication for the Utility Large watch face.

Swordee
  • 11
  • 1
  • Not related to your question but you should not use a fixed dateFormat when displaying time to the user. You should respect the user device's locale and settings using dateFormatter timeStyle to display a localized time. – Leo Dabus Mar 08 '20 at 00:48
  • @LeoDabus Thanks for the information. However, this is just a personal project as I want digital time and analog time showing on my Apple Watch. In fact, I only took the time to learn basic Swift just to accomplish this goal—silly, I know. If I ever make some sort of public Swift software, I'll be sure to take that into account. – Swordee Mar 08 '20 at 00:55
  • To provide data for a complication you need to provide data for [its timeline](https://developer.apple.com/documentation/clockkit/adding_a_complication_to_your_watchos_app/providing_data_for_your_complication/updating_your_timeline). You can't update the timeline very often (once per hour if you use a refresh task), but in your case you know the data to add to the timeline; A new `date` instance every minute for the next x minutes. As an aside, you could probably just use the in-built word time complication set to your home city – Paulw11 Mar 08 '20 at 01:40
  • 1
    @Paulw11 That makes sense; I didn't think about it like that before—thanks! How would I go about updating the timeline? Sorry for the questions, it's just hard to find information about developing complications. – Swordee Mar 08 '20 at 01:59

1 Answers1

0

As mentioned by @Paulw11, you should provide a Timeline for the complication. You can do this in the Methode "getTimelineEntries" in the class "ComplicationController":

func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        // Call the handler with the timeline entries after to the given date
var entries = [CLKComplicationTimelineEntry]()

        for i in 0...(limit) {
            var futureDate = date
            futureDate.addTimeInterval(TimeInterval(60 * i))
            switch complication.family {
            case .utilitarianLarge:
                let template = CLKComplicationTemplateUtilitarianLargeFlat()

                let currentDateTime = Date()
                let formatter = DateFormatter()
                formatter.dateFormat = "h:mm a"
                let dateTimeString = formatter.string(from: futureDate)
                let timeLineText = dateTimeString

                template.textProvider = CLKSimpleTextProvider(text: "\(timeLineText)")

                let timelineEntry = CLKComplicationTimelineEntry(date: futureDate, complicationTemplate: template)
                entries.append(timelineEntry)
            default:
                handler(nil)
            }
        }
        handler(entries)
    }
}

For Updating the Complication Timeline you can use the CLKComplicationServer.sharedInstance().extendTimeline() or CLKComplicationServer.sharedInstance().reloadTimeline() Methode.

You can try to call them in a BackgroundAppRefresh Task. I'm trying the same as you at the Moment (build a complication who shows the Time as String "HH:MM" in my case), but the BackgroundAppRefresh only works if the AppleWatch is in the same Wireless Network as the iPhone. If someone has any idea how to fix this issue I would be very thankful!

3xypn0s
  • 80
  • 6