【问题标题】:How do I refresh WatchApp complications如何刷新 WatchApp 并发症
【发布时间】:2021-06-23 20:17:06
【问题描述】:

所以我尝试使用Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in 每秒更新headerTextProvider:

我猜这不是正确的做法?

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

【问题讨论】:

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


    【解决方案1】:

    通常,您不会将时间线条目“推送”到您的并发症中。您在 ClockKit 请求时提供时间线条目。

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

    更多详情请见Keeping Your Complications Up to Date

    使用Timer 并不是一种真正有意义的方法; Timer 只会在您的手表应用在前台时触发,如果它在前台,则不会显示您的并发症。

    您可以在数据源中实现getTimelineEndDategetTimelineEntries;然后当您的数据源被要求输入条目时,您可以提供未来的条目以显示并发症。

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-14
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多