Ios 每分钟更新当前时间(Swift)

Ios 每分钟更新当前时间(Swift),ios,swift,watchkit,watchos,Ios,Swift,Watchkit,Watchos,目前,我的代码仅在首次启动时更新(在本例中,启动Apple Watch)。我希望当前时间每分钟自动更新一次,这样就可以读为h:mm a if complication.family == .utilitarianLarge { let template = CLKComplicationTemplateUtilitarianLargeFlat() let currentDateTime = Date() let formatter = Date

目前,我的代码仅在首次启动时更新(在本例中,启动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)
    }

关于更多内容,这是一个复杂的实用大型表面。

如@Paulw11所述,您应该提供复杂的时间线。您可以在类“CompliationController”中的方法“getTimelineEntries”中执行此操作:

要更新复杂度时间线,可以使用
CLKComplicationServer.sharedInstance().extendTimeline()
CLKComplicationServer.sharedInstance().reloadTimeline()
方法

你可以试着打电话给他们做背景调查。我现在也在尝试和你一样的方法(在我的例子中,构建一个将时间显示为字符串“HH:MM”的复杂程序),但是只有当AppleWatch与iPhone位于同一无线网络中时,BackgroundAppRefresh才起作用。
如果有人知道如何解决这个问题,我将非常感谢

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

要更新复杂度时间线,可以使用
CLKComplicationServer.sharedInstance().extendTimeline()
CLKComplicationServer.sharedInstance().reloadTimeline()
方法

你可以试着打电话给他们做背景调查。我现在也在尝试和你一样的方法(在我的例子中,构建一个将时间显示为字符串“HH:MM”的复杂程序),但是只有当AppleWatch与iPhone位于同一无线网络中时,BackgroundAppRefresh才起作用。
如果有人知道如何解决这个问题,我将非常感谢

与问题无关,但向用户显示时间时不应使用固定日期格式。您应该尊重用户设备的区域设置和设置,使用dateFormatter timeStyle显示本地化的时间。@LeoDabus感谢您提供的信息。然而,这只是一个个人项目,因为我想在我的苹果手表上显示数字时间和模拟时间。事实上,我只是花时间学习基本的Swift,只是为了完成这个愚蠢的目标,我知道。如果我曾经制作过某种公共Swift软件,我一定会考虑到这一点。要为复杂情况提供数据,你需要为复杂情况提供数据。您不能经常更新时间线(如果使用刷新任务,则每小时更新一次),但在您的情况下,您知道要添加到时间线的数据;在接下来的x分钟内,每分钟将有一个新的
date
实例。顺便说一句,你可以只使用你家里内置的单词时间复合词city@Paulw11这是有道理的;我以前没这么想过谢谢!我将如何更新时间线?对于这些问题,很抱歉,很难找到有关发展复杂度的信息。与您的问题无关,但您不应在向用户显示时间时使用固定日期格式。您应该尊重用户设备的区域设置和设置,使用dateFormatter timeStyle显示本地化的时间。@LeoDabus感谢您提供的信息。然而,这只是一个个人项目,因为我想在我的苹果手表上显示数字时间和模拟时间。事实上,我只是花时间学习基本的Swift,只是为了完成这个愚蠢的目标,我知道。如果我曾经制作过某种公共Swift软件,我一定会考虑到这一点。要为复杂情况提供数据,你需要为复杂情况提供数据。您不能经常更新时间线(如果使用刷新任务,则每小时更新一次),但在您的情况下,您知道要添加到时间线的数据;在接下来的x分钟内,每分钟将有一个新的
date
实例。顺便说一句,你可以只使用你家里内置的单词时间复合词city@Paulw11这是有道理的;我以前没这么想过谢谢!我将如何更新时间线?很抱歉提出这些问题,只是很难找到关于发展中并发症的信息。
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)
    }
}