我想先说的是,我知道有这样一个问题,但我还没有找到任何关于我读过的,解决我的问题。
我的最终目标是拥有一个像倒计时钟一样的手表复杂功能。我有一个iOS应用程序,它可以从socketIO文件中读取信息,并将该文件中的信息存储在GlobalVariable.swift文件中。
在我的iOS ContentView中,我从GlobalVarible中读取并显示它。我还从同一个GlobalVarible文件中读取并在WatchKit扩展-> ContentView上显示此信息。
这是工作,但当我试图得到的信息,我的ComplicationController的socketIO信息还没有改变的变量,所以我需要安排新的条目首选每一秒,但我可以做一个变通办法,如果我可以让它工作,每分钟更新。
有人告诉我,我可以使用getTimelineEntries
,但如果我在这个链接中链接,它将加载100个条目并更新100次,但从我的GlobalVaribel文件的信息存储第一秒或默认信息100次。这种方法对我不起作用。How do I refresh WatchApp complications
这让我想到了一个CreatingAndUpdatingAComplicationsTimeline
的帖子,附带了一个项目,我可以看看并尝试弄清楚事情。在该文档和另一篇文章中,我可以读到我应该使用“ExtensionDelegate”
现在的问题是,这个文件和它的所有内容从来没有触发,我不知道从哪里开始我的搜索如何让它触发?我尝试在这个委托中记录所有不同的函数,但没有触发,这就是为什么我认为文件没有加载到应用程序中。
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
A delegate object for the WatchKit extension that implements the needed life cycle methods.
*/
import ClockKit
import WatchKit
import os
// The app's extension delegate.
class ExtensionDelegate: NSObject, WKExtensionDelegate {
func applicationDidFinishLaunching(){
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
}
func applicationDidBecomeActive(){
print("--------------------------------------------------")
print("applicationDidBecomeActive")
print("--------------------------------------------------")
}
func applicationWillResignActive(){
print("--------------------------------------------------")
print("applicationWillResignActive")
print("--------------------------------------------------")
}
func applicationWillEnterForeground(){
print("--------------------------------------------------")
print("applicationWillEnterForeground")
print("--------------------------------------------------")
}
func applicationDidEnterBackground(){
print("--------------------------------------------------")
print("applicationDidEnterBackground")
print("--------------------------------------------------")
}
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
print("__________________________________________________")
print("Handling a background task...")
print("App State: \(WKExtension.shared().applicationState.rawValue)")
print("__________________________________________________")
for task in backgroundTasks {
print("++++++++++++++++++++++++++++++++++++++++++++++++++")
print("Task: \(task)")
print("++++++++++++++++++++++++++++++++++++++++++++++++++")
switch task {
// Handle background refresh tasks.
case let backgroundTask as WKApplicationRefreshBackgroundTask:
scheduleBackgroundRefreshTasks()
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("**************************************************")
print("Scheduling a background task.")
print("**************************************************")
let watchExtension = WKExtension.shared()
let targetDate = Date().addingTimeInterval(3*60)
watchExtension.scheduleBackgroundRefresh(withPreferredDate: targetDate, userInfo: nil) { (error) in
if let error = error {
print("An error occurred while scheduling a background refresh task: \(error.localizedDescription)")
return
}
print("Task scheduled!")
}
}
整个项目都在这里
https://github.com/mattehalen/Scheduled-countdown-WatchKit/tree/iPhone%26WatchApp
1条答案
按热度按时间tf7tbtn21#
事实证明,我使用的是SwiftUI 2.0和SwiftApp生命周期。
为了解决这个问题,我需要将下面的代码添加到我的
Scheduled_countdownApp.swift
中对于
@Environment
,我可以使用下面的代码,但因为我已经有了ExtensionDelegate,所以我只是将其注解掉。整个代码