每分钟更新当前时间(快速)

问题描述 投票:1回答:1

当前,我的代码仅在首次启动时更新(在这种情况下,启动Apple Watch)。我希望当前时间每分钟自动更新一次,因此可以理解为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)
    }

有关更多信息,这是Utility Large表盘的复杂之处。

ios swift watchkit watchos
1个回答
0
投票

如@ Paulw11所提到的,您应该为并发症提供时间表。您可以在类“ ComplicationController”中的Methode“ getTimelineEntries”中执行此操作:

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()方法。

您可以尝试在BackgroundAppRefresh任务中调用它们。我正在尝试与您进行相同的操作(构建一个复杂的功能,在我的案例中,该功能将时间显示为字符串“ HH:MM”),但是BackgroundAppRefresh仅在AppleWatch与iPhone位于同一无线网络中时才有效。如果有人对如何解决此问题有任何想法,我将非常感激!

© www.soinside.com 2019 - 2024. All rights reserved.