ios - 调用 ExtensionDelegate 为 Complication 创建/刷新数据

标签 ios swift watchkit watchos-2 apple-watch-complication

我所有的数据创建都是在 ExtensionDelegate.swift 中完成的。

问题是 ExtensionDelegate.swift 在我的 ComplicationController.swift 函数 getCurrentTimelineEntryForComplication 之前没有被调用.

有什么想法吗? 这是我的代码和详细信息:

所以我的数组 extEvnts 在我的 ComplicationController.swift 中是空的:

    func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {

        let extEvnts = ExtensionDelegate.evnts
}

因为我的 ExtensionDelegate.swift 还没有被调用,这就是为数组创建数据的原因:

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

    private let session = WCSession.defaultSession()
    var receivedData = Array<Dictionary<String, String>>()
    static var evnts = [Evnt]()

    func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

        if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {

            receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
            ExtensionDelegate.evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))

        } else {
            print("tColorValue and matchValue are not same as dictionary value")
        }

    }

    func applicationDidFinishLaunching() {
        // Perform any final initialization of your application.
        if WCSession.isSupported() {
            session.delegate = self
            session.activateSession()
        }
    }
}

编辑:

Per Apple,看起来这与它有关,但出于某种原因我不知道如何实际实现它,因为我无法调用 mydelegate.evnts:

// Get the complication data from the extension delegate.
let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
var data : Dictionary = myDelegate.myComplicationData[ComplicationCurrentEntry]!

所以我已经尝试过类似的方法,但仍然无法正常工作,因为我仍然没有收到任何数据:

func someMethod() {
    let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
    let dict = ExtensionDelegate.evnts
    print("ExtensionDel.evnts: \(dict.count)")

最佳答案

对我有帮助的有用问题 here

在函数 requestedUpdateDidBegin() 中,您可以更新将在复杂功能中显示的信息。因此,在此方法中,您可以使用 WatchConnectivity 方法(如 sendMessage:replyHandler:errorHandler:)调用您的父应用程序接收新信息。

您可以使用 NSUserDefaults 来存储将在您的 ComplicationController 中使用的命令式数据,然后从 NSUserDefaults 为您的复杂功能加载此信息.我将此数据存储在用户默认值中,以便在新数据无法加载时始终显示旧数据。

关于ios - 调用 ExtensionDelegate 为 Complication 创建/刷新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542729/

相关文章:

ios - 我如何测试 View Controller 是否已呈现?

ios - 如何让 UIViewController 弹出 Swift SWRevealViewController?

swift - 异步自动释放池

swift - WatchOS:通过 "Next Page"segue 将数据从一个 Controller 传递到另一个 Controller

swift - WatchKit 和地理定位

ios - 导航栏不出现

ios - Kinvey 关于 IOS 模拟器问题

ios - Swift:UIBeizerPath CAShapelayer 填充时填充评论

ios - 'openParentApplication(_ :reply:)' has been explicitly marked unavailable here - Xcode 7 Beta

ios - swift3中的CocoaAsyncSocket创建tcp服务器/客户端?