微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ExtensionDelegate.swift 未调用

如何解决ExtensionDelegate.swift 未调用

首先我想说我知道有一个类似的问题,但在我读过的那些问题中没有找到任何可以解决我的问题的内容

我的最终目标是拥有一个像倒计时时钟一样的手表复杂功能我有一个 iOS 应用程序,它从 socketIO 文件中读取信息并将该文件中的信息存储在 GlobalVarible.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

解决方法

事实证明,我使用的是 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 扩展 // // 由 Mathias Halén 于 2021-06-22 创建。 // 版权所有 © 2021 Mathias Halén。版权所有。 // 导入 SwiftUI @主要的 struct Scheduled_countdownApp:应用{ @WKExtensionDelegateAdaptor(ExtensionDelegate.self) var 委托 //@UIApplicationDelegateAdaptor(ExtensionDelegate.self) var appDelegate @Environment(\.scenePhase) var scenePhase @SceneBuilder var body:一些场景{ 窗口组{ 导航视图{ 内容视图() } } // .onChange(of:scenePhase) { 进入 // 切换阶段{ // case .active: // 打印("->->->->->->->->->->->->->->->->->->->->->->- >->->->->->->->->") // 打印(“活动”) // 打印("->->->->->->->->->->->->->->->->->->->->->->- >->->->->->->->->") // case .inactive: // 打印("->->->->->->->->->->->->->->->->->->->->->->- >->->->->->->->->") // 打印(“无效”) // 打印("->->->->->->->->->->->->->->->->->->->->->->- >->->->->->->->->") //案例.背景: // 打印("->->->->->->->->->->->->->->->->->->->->->->- >->->->->->->->->") // 打印(“背景”) // 打印("->->->->->->->->->->->->->->->->->->->->->->- >->->->->->->->->") // @unknown 默认值: // 打印(“苹果新添加的东西”) // } // } WKNotificationScene(控制器:NotificationController.self,类别:“myCategory”) } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。