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

时间:2017-05-22 02:42:08

标签: watchkit apple-watch apple-watch-complication

目前,我的代码不起作用。我仍然需要手动刷新它。 我希望复杂功能每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))


}

2 个答案:

答案 0 :(得分:0)

您可以使用以下功能填充数据并发症。

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

答案 1 :(得分:0)

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

  

在预定更新开始时,ClockKit会调用   requestedUpdateDidBegin或requestedUpdateBudgetExhausted方法,   取决于您的并发症的时间预算的状态。你必须   如果要向您的数据添加数据,请实施其中一种或两种方法   时间线。您应该扩展或重新加载这些方法的实现   根据需要复杂化的时间表。当你这样做,   ClockKit从您的数据源请求新的时间轴条目。如果   你不延长或重新加载时间线,ClockKit不要求   任何新的时间表条目。

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

有关详细信息,请查看this