如何刷新 WatchApp 并发症 [英] How do I refresh WatchApp complications

查看:33
本文介绍了如何刷新 WatchApp 并发症的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用 Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in

我猜这不是正确的做法吗?

I guess that this is not the right way of doing this?

    // MY STUFF HERE
    //----------------------------------------------------------------------------------------------------
    func getCurrentTimelineEntry(for complication: CLKComplication,
                                 withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {

        @ObservedObject var service = Service()
        print("-------------------->>>>>>>>>> \(service.currentTime)")
        let date = Date()
        //let currentTime = service.currentTime
        let title = service.title
        let time = service.time

        var template: CLKComplicationTemplate!
        let line1 = CLKSimpleTextProvider(text:title)
        let line2 = CLKSimpleTextProvider(text:time)
        //let line3 = CLKSimpleTextProvider(text:currentTime)

        if complication.family == .modularLarge {
            template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: line1, body1TextProvider: line2)
            let entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
            handler(entry)
        }
        else{
            handler(nil)
        }
    }

全码

//
//  ComplicationController.swift
//  WatchApp WatchKit Extension
//
//  Created by Mathias Halén on 2021-06-22.
//  Copyright © 2021 Mathias Halén. All rights reserved.
//

import ClockKit
import SwiftUI


class ComplicationController: NSObject, CLKComplicationDataSource {
    final class Service: ObservableObject{
        static let sharedInstance1 = Service()
        @Published var currentTime = " first time"
        @Published var title = "Hello"
        @Published var time = "World"
        
        init() {
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
                print("----------> WatchOS -> ComplicationController \(Date())")
                self.currentTime = GlobalVaribles.sharedInstance.currentTime
                self.title = GlobalVaribles.sharedInstance.title
                self.time = GlobalVaribles.sharedInstance.time
            }
        }
    }
    
    // MARK: - Complication Configuration
    func getComplicationDescriptors(handler: @escaping ([CLKComplicationDescriptor]) -> Void) {
        
        let descriptors = [
            CLKComplicationDescriptor(identifier: "complication",
                                      displayName: "Test 2",
                                      supportedFamilies: CLKComplicationFamily.allCases)
            // Multiple complication support can be added here with more descriptors
        ]
        
        // Call the handler with the currently supported complication descriptors
        handler(descriptors)
    }
    func handleSharedComplicationDescriptors(_ complicationDescriptors: [CLKComplicationDescriptor]) {
        // Do any necessary work to support these newly shared complication descriptors
    }
    // MARK: - Timeline Configuration
    func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
        // Call the handler with the last entry date you can currently provide or nil if you can't support future timelines
        let currentDate = Date()
        handler(currentDate)
    }
    func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
        // Call the handler with your desired behavior when the device is locked
        handler(.showOnLockScreen)
    }
    func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
        // This method will be called once per supported complication, and the results will be cached
        handler(nil)
    }
    // MY STUFF HERE
    //----------------------------------------------------------------------------------------------------
    func getCurrentTimelineEntry(for complication: CLKComplication,
                                 withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        
        @ObservedObject var service = Service()
        print("-------------------->>>>>>>>>> \(service.currentTime)")
        let date = Date()
        //let currentTime = service.currentTime
        let title = service.title
        let time = service.time
        
        var template: CLKComplicationTemplate!
        let line1 = CLKSimpleTextProvider(text:title)
        let line2 = CLKSimpleTextProvider(text:time)
        //let line3 = CLKSimpleTextProvider(text:currentTime)
        
        if complication.family == .modularLarge {
            template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: line1, body1TextProvider: line2)
            let entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
            handler(entry)
        }
        else{
            handler(nil)
        }
    }
   
}

推荐答案

通常,您不要推"时间线条目到您的并发症.您可以在 ClockKit 请求时提供时间线条目.

As a rule, you don't "push" timeline entries to your complication. You supply timeline entries when ClockKit requests them.

您可以使用 reloadTimelineextendTimeline 触发来自ClockKit,但您通常只会定期使用它;假设用户交互何时导致您的应用加载新数据,或者可能基于计划的刷新任务.

You can use reloadTimeline and extendTimeline to trigger a request from ClockKit, but you would normally only use this periodically; Say when user interaction causes your app to load new data or perhaps based on a scheduled refresh task.

有关详细信息,请参阅让您的并发症保持最新状态.

See Keeping Your Complications Up to Date for more details.

使用 Timer 并不是真正有意义的方法;Timer 只会在您的手表应用处于前台时触发,如果它在前台,则不会显示您的复杂功能.

Using a Timer isn't really an approach that makes sense; The Timer will only fire when your watch app is in the foreground, and if it is in the foreground then your complication isn't being show.

你可以做的是在你的数据源中实现 getTimelineEndDategetTimelineEntries ;然后,当要求您的数据源输入条目时,您可以提供将来要显示的复杂功能的条目.

What you can do is implement getTimelineEndDate and getTimelineEntries in your data source; then when your data source is asked for entries you can supply future entries for the complication to show.

首先,提供.distantFuture作为结束日期,表示您的并发症可以无限期地提供数据.

First, supply .distantFuture for the end date, indicating that your complication can provide data indefinitely.

func getTimelineEndDate(for complication: CLKComplication, 
                     withHandler handler: @escaping (Date?) -> Void) {
    handler(.distantFuture)
}

其次,提供一系列时间线条目,例如,接下来的 3600 秒中的每一秒都有一个条目.

Second, supply a series of time line entries, one for each of the next 3600 seconds, say.

func getTimelineEntries(for complication: CLKComplication, 
                           after date: Date, 
                           limit: Int, 
                     withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {

    guard complication.family == .modularLarge else {
        handler(nil)
        return
    }

    let now = Date()
    var entries = [CLKComplicationTimelineEntry]()
    let line1 = CLKSimpleTextProvider(text:title)
    for i in 0..<3600 {
        let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: line1, body1TextProvider: line2)
        let line2 = CLKSimpleTextProvider(text:"\(i)")
        let entry = CLKComplicationTimelineEntry(date: date.addingTimeInterval(Double(i)), complicationTemplate: template)
            entries.append(entry)
    }
    completion(entries)
}

这篇关于如何刷新 WatchApp 并发症的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆