Обновлять текущее время каждую минуту (Swift) - PullRequest
1 голос
/ 08 марта 2020

В настоящее время мой код обновляется только при первом запуске (в данном случае при загрузке Apple Watch). Я хочу, чтобы текущее время автоматически обновлялось каждую минуту, чтобы оно могло быть прочитано как ч: мм а.

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)
    }

Для большего контекста это осложнение для циферблата Utility Large.

1 Ответ

0 голосов
/ 08 марта 2020

Как упомянуто @ Paulw11, вы должны предоставить график времени для осложнения. Вы можете сделать это в Methode «getTimelineEntries» в классе «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)
    }
}

Для обновления временной шкалы сложности вы можете использовать метод CLKComplicationServer.sharedInstance().extendTimeline() или CLKComplicationServer.sharedInstance().reloadTimeline().

Вы можете попытаться вызвать их в задании BackgroundAppRefre sh. Я пытаюсь сделать то же самое, что и вы, в момент (создайте усложнение, которое в моем случае показывает время в виде строки "ЧЧ: ММ"), но BackgroundAppRefre sh работает, только если AppleWatch находится в той же беспроводной сети, что и iPhone. Если у кого-то есть идеи, как решить эту проблему, я был бы очень благодарен!

...