我想首先说,我知道有这样一个问题,但我在读过的文章中没有找到任何解决我问题的东西。
我的最终目标是有一个复杂的手表像倒计时钟。我有一个iOS应用程序,它从socketIO文件中读取信息,并将该文件中的信息存储在GlobalVarible.swift文件中。
在我的iOS内容视图中,我读取GlobalVarible并显示它。我还从相同的GlobalVarible文件中读取并在WatchKit扩展上显示该信息->ContentView。
这是有效的,但当我试图将信息获取到我的ComplicationController时,socketIO信息还没有改变变量,所以我需要安排每秒首选的新条目,但如果我能通过每分钟更新来让它发挥作用,我可以做一个变通办法。
有人向我展示了我可以使用getTimelineEntries
,但如果我在这个链接中链接,它会加载100个条目并更新100次,但我的GlobalVaribel文件中的信息会存储第一秒或默认信息100次。这种方法对我不起作用。我如何刷新WatchApp复杂
这让我看到了CreatingAndUpdatingAComplicationsTimeline
的帖子,其中附带了一个项目,我可以查看并尝试解决问题。在那份文件和另一篇帖子中,我读到我应该使用`ExtensionDelegate`
现在的问题是,这个文件和其中的所有内容从未触发,我不知道从哪里开始搜索如何触发它?我试图记录该委托中的所有不同函数,但NONE正在触发,这就是为什么我认为该文件没有加载到应用程序中。
/*
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原来我使用的是SwiftUI 2.0和SwiftApp生命周期。
为了解决这个问题,我需要将下面的代码添加到我的Scheduled_countdownApp.swift
中
@WKExtensionDelegateAdaptor(ExtensionDelegate.self) var delegate
@Environment(.scenePhase) var scenePhase
有了@Environment
,我可以使用下面的代码,但因为我已经有了ExtensionDelegate,所以我只是注释掉了它。
// .onChange(of: scenePhase) { phase in
// switch phase{
// case .active:
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// print("Active")
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// case .inactive:
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// print("inactive")
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// case .background:
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// print("background")
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// @unknown default:
// print("something new added by apple")
// }
// }
完整代码
// Scheduled_countdownApp.swift
// WatchApp WatchKit Extension
//
// Created by Mathias Halén on 2021-06-22.
// Copyright © 2021 Mathias Halén. All rights reserved.
//
import SwiftUI
@main
struct Scheduled_countdownApp: App {
@WKExtensionDelegateAdaptor(ExtensionDelegate.self) var delegate
//@UIApplicationDelegateAdaptor(ExtensionDelegate.self) var appDelegate
@Environment(.scenePhase) var scenePhase
@SceneBuilder var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
// .onChange(of: scenePhase) { phase in
// switch phase{
// case .active:
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// print("Active")
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// case .inactive:
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// print("inactive")
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// case .background:
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// print("background")
// print("->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->")
// @unknown default:
// print("something new added by apple")
// }
// }
WKNotificationScene(controller: NotificationController.self, category: "myCategory")
}
}