Swift Can';我不了解苹果手表的复杂生命周期&;让它更新

Swift Can';我不了解苹果手表的复杂生命周期&;让它更新,swift,watchkit,apple-watch,apple-watch-complication,watchos-5,Swift,Watchkit,Apple Watch,Apple Watch Complication,Watchos 5,我知道有很多问题和例子,但我仍然不明白到底是怎么回事,所以请帮助我 以下是我在watch os 5至7上运行和更新复杂度的代码: 我想每小时或更短的时间更新我的并发症 我应该实现的设置功能如下: 这是我的“复杂控制器”课程: func getLocalizableSampleTemplate(对于复杂度:CLKComplication,withHandler处理程序:@escaping(CLKComplicationTemplate?)->Void){ 让future=localDate()。添

我知道有很多问题和例子,但我仍然不明白到底是怎么回事,所以请帮助我

以下是我在watch os 5至7上运行和更新复杂度的代码:

我想每小时或更短的时间更新我的并发症

我应该实现的设置功能如下:

这是我的“复杂控制器”课程:

func getLocalizableSampleTemplate(对于复杂度:CLKComplication,withHandler处理程序:@escaping(CLKComplicationTemplate?)->Void){
让future=localDate()。添加时间间隔(15.0*60.0*60.0)
let template=createTemplate(用于复杂度:复杂度,日期:未来)
处理程序(模板)
}
func getCurrentTimelineEntry(对于复杂度:CLKComplication,带处理程序处理程序:@escaping(CLKComplicationTimelineEntry?)->Void){
处理程序(createTimelineEntry(用于复杂性:复杂性,日期:日期())
}
func getTimelineStartDate(对于复杂度:CLKComplication,withHandler:@escaping(Date?)->Void){
处理程序(无)
}
func getPrivacyBehavior(对于复杂度:CLKComplication,withHandler处理程序:@escaping(CLKComplicationPrivacyBehavior)->Void){
处理程序(.showOnLockScreen)
}
func getTimelineEndDate(对于复杂度:CLKComplication,withHandler:@escaping(Date?)->Void){
处理程序(日期().addingTimeInterval(24.0*60.0*60.0))
}
func getSupportedTimeTravelDirections(对于复杂度:CLKComplication,带处理程序处理程序:@escaping(CLKComplicationTimeTravelDirections)->Void){
处理程序([.forward])
}
func getTimelineEntries(对于复杂度:CLKComplication,在日期:日期之后,限制:Int,withHandler处理程序:@escaping([CLKComplicationTimelineEntry]?)->Void){
设五分钟=15.0*60.0
假设二十四小时=24.0*60.0*60.0
变量条目=[CLKComplicationTimelineEntry]()
var current=日期。添加时间间隔(五分钟)
让endDate=date.addingTimeInterval(二十四小时)
while(current.compare(endDate)=.orderedAscending)和&(entries.countVoid){
打印(“CompliationController GetCompliationDescriptors调用”)
设描述符=[
CLKComplicationDescriptor(标识符:“并发症”,显示名称:“AAAA”,支持的族:CLKComplicationFamily.allCases)
]
处理程序(描述符)
}
@可用(watchOSApplicationExtension 7.0,*)
func HandleSharedCompliationDescriptors(CompliationDescriptors:[CLKComplicationDescriptor]){
}
private func createTemplate(用于复杂度:CLKComplication,日期:date)->CLKComplicationTemplate?{
var平台=空平台
如果让ps=extensionelegate.platforms{
如果先设p=ps{
平台=p
}
}否则{
LOADOFFLINE平台{(平台)位于
如果设p=plats,则首先{
平台=p
}
}
}
交换家庭{
案例.模块小:
返回CreateModulerSmallTemplate(平台:平台)
案例.实用小型,.实用小型公寓:
返回createUtilitarianSmallFlatTemplate(平台:平台)
案例.通告小:
返回createCircularSmallTemplate(平台:平台)
案例.功利主义大:
return CreateUtilitarianalLarGetTemplate(平台:平台)
案例.图表角:
返回createGraphicCornerTemplate(平台:平台)
案件.通告:
返回createGraphicCircleTemplate(平台:平台)
案例.矩形:
返回createGraphicRectangularTemplate(平台:平台)
外壳。图形边框:
返回createGraphicBezelTemplate(平台:平台)
案例.特大型:
返回CreateExtralarGetTemplate()
案例。模块大:
返回CreateModulerArgeTemplate(平台:平台)
案例.超大:
如果可用(watchOSApplicationExtension 7.0,*){
返回CreateGraphicExtralarGetTemplate(平台:平台)
}否则{
fatalError(“图形超大模板仅在watchOS 7上可用。”)
返回CreateModulerSmallTemplate(平台:平台)
}
@未知默认值:
fatalError(“***未知并发症系列***”)
归零
}
}
这是我的ExtensionLegate类:

 static var platforms:[Platform]!
 func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    print("ExtensionDelegate handle called")
    for task in backgroundTasks {
        switch task {
        // Handle background refresh tasks.
        case let backgroundTask as WKApplicationRefreshBackgroundTask:
            WatchConnectivityHelper().getPlatforms { (platforms) in
                ExtensionDelegate.platforms = platforms
                self.scheduleBackgroundRefreshTasks()
                let server = CLKComplicationServer.sharedInstance()
                if let compli = server.activeComplications {
                    for complication in compli {
                        server.reloadTimeline(for: complication)
                    }
                }
                backgroundTask.setTaskCompletedWithSnapshot(true)
            }
            
        case let snapshotTask as WKSnapshotRefreshBackgroundTask:
            snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
        case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
            connectivityTask.setTaskCompletedWithSnapshot(false)
        case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
            urlSessionTask.setTaskCompletedWithSnapshot(false)
        case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
            relevantShortcutTask.setTaskCompletedWithSnapshot(false)
        case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
            intentDidRunTask.setTaskCompletedWithSnapshot(false)
        default:
            task.setTaskCompletedWithSnapshot(false)
        }
    }
}

func scheduleBackgroundRefreshTasks() {
    print("ExtensionDelegate scheduleBackgroundRefreshTasks called")
    let watchExtension = WKExtension.shared()
    let targetDate = Date().addingTimeInterval(15.0 * 60.0)
    watchExtension.scheduleBackgroundRefresh(withPreferredDate: targetDate, userInfo: nil) { (error) in
        if let error = error {
            print("*** An background refresh error occurred: \(error.localizedDescription) ***")
            return
        }
        print("*** Background Task Completed Successfully! ***")
    }
}
静态变量平台:[平台]!
func句柄(uuBackgroundTasks:Set){
打印(“已调用ExtensionLegate句柄”)
用于背景任务中的任务{
切换任务{
//处理后台刷新任务。
案例let backgroundTask作为WKApplicationRefreshBackgroundTask:
watchConnectionyHelper().getPlatforms{(平台)位于
extensionelegate.platforms=平台
self.scheduleBackgroundRefreshTasks()
让server=CLKComplicationServer.sharedInstance()
如果让compli=server.active{
合并症{
server.reloadTimeline(用于:复杂度)
}
}
backgroundTask.setTaskCompletedWithSnapshot(true)
}
案例让snapshotTask作为WKSnapshotRefreshBackgroundTask:
snapshotTask.setTaskCompleted(restoredDefaultState:true,estimatedSnapshotExpiration:Date.distantFuture,userInfo:nil)
案例let connectivityTask as wkWatch connectivityRefreshBackgroundTask:
 static var platforms:[Platform]!
 func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    print("ExtensionDelegate handle called")
    for task in backgroundTasks {
        switch task {
        // Handle background refresh tasks.
        case let backgroundTask as WKApplicationRefreshBackgroundTask:
            WatchConnectivityHelper().getPlatforms { (platforms) in
                ExtensionDelegate.platforms = platforms
                self.scheduleBackgroundRefreshTasks()
                let server = CLKComplicationServer.sharedInstance()
                if let compli = server.activeComplications {
                    for complication in compli {
                        server.reloadTimeline(for: complication)
                    }
                }
                backgroundTask.setTaskCompletedWithSnapshot(true)
            }
            
        case let snapshotTask as WKSnapshotRefreshBackgroundTask:
            snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
        case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
            connectivityTask.setTaskCompletedWithSnapshot(false)
        case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
            urlSessionTask.setTaskCompletedWithSnapshot(false)
        case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
            relevantShortcutTask.setTaskCompletedWithSnapshot(false)
        case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
            intentDidRunTask.setTaskCompletedWithSnapshot(false)
        default:
            task.setTaskCompletedWithSnapshot(false)
        }
    }
}

func scheduleBackgroundRefreshTasks() {
    print("ExtensionDelegate scheduleBackgroundRefreshTasks called")
    let watchExtension = WKExtension.shared()
    let targetDate = Date().addingTimeInterval(15.0 * 60.0)
    watchExtension.scheduleBackgroundRefresh(withPreferredDate: targetDate, userInfo: nil) { (error) in
        if let error = error {
            print("*** An background refresh error occurred: \(error.localizedDescription) ***")
            return
        }
        print("*** Background Task Completed Successfully! ***")
    }
}