如何自动更新 Apple Watch 并发症?

How to update Apple Watch complications automatically?

目前,我的代码不起作用。我仍然必须手动刷新它。 我希望并发症每 12 小时自动更新一次。

func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {

    let date = Calendar.current.startOfDay(for: Date())
    print("timeline start date :\(date)")
    handler(date)
}

func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
    var  date = Calendar.current.startOfDay(for: Date())
    date = Calendar.current.date(byAdding: .day, value: 2, to: date)!
    print("timeline end date:\(date)")
    handler(date)
}

func getNextRequestedUpdateDate(handler: @escaping (Date?) -> Void){
    handler(Date(timeIntervalSinceNow: 60*60*12))


}

您可以使用以下函数用数据填充复杂功能。

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
    // Call the handler with the current timeline entry
    handler(nil)
}

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

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
    handler(nil)
}

App Programming Guide for watchOS

数据源方法似乎没有实现。刷新需要实现。

At the start of a scheduled update, ClockKit calls either the requestedUpdateDidBegin or requestedUpdateBudgetExhausted method, depending on the state of your complication’s time budget. You must implement one or both of those methods if you want to add data to your timeline. Your implementation of those methods should extend or reload the timeline of your complication as needed. When you do that, ClockKit requests the new timeline entries from your data source. If you do not extend or reload your timeline, ClockKit does not ask for any new timeline entries.

func requestedUpdateDidBegin() {
    let server=CLKComplicationServer.sharedInstance()
    for complication in server.activeComplications {
        server.reloadTimelineForComplication(complication)
    }
}

有关详细信息,请查看 this