0

WCSession を介してコンプリケーションを更新したいのですが、メモが機能しているようです。10 分ごとにコンプリケーションを更新しています (これが最大であることがわかりました)。しかし、何も起こりません。誰でも私のコードで問題を見ることができますか?

更新に必要なデリゲートを実装しました。ここでの問題は何ですか?どうもありがとうございました!

import ClockKit import WatchConnectivity

class ComplicationController: NSObject, CLKComplicationDataSource, WCSessionDelegate {

    var session : WCSession!
    var level = 0;

    // MARK: - Timeline Configuration

    func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
        //handler([.forward, .backward])
        handler([])
    }

    func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
        handler(nil)
    }

    func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
        handler(nil)
    }

    func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
        handler(.showOnLockScreen)
    }

    // MARK: - Timeline Population

    func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        // Call the handler with the current timeline entry

        switch complication.family {

        case .modularSmall:
            let template = CLKComplicationTemplateModularSmallRingText()
            template.textProvider = CLKSimpleTextProvider(text: "\(level)")
            let result = self.level / 100;
            template.fillFraction = Float(result)
            handler(CLKComplicationTimelineEntry(date: NSDate() as Date, complicationTemplate: template))
        default:
            NSLog("%@", "Unknown complication type: \(complication.family)")
            handler(nil)
        }

        handler(nil)
    }

    func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
        handler(NSDate(timeIntervalSinceNow: 600))
    }

    func requestedUpdateDidBegin() {
        if (WCSession.isSupported()) {
            session = WCSession.default()
            session.delegate = self
            session.activate()
        }

        if(session.activationState.rawValue == 2){
            checkValue{(isResponse) -> Void in
                self.level = isResponse;
                let server=CLKComplicationServer.sharedInstance()
                for complication in server.activeComplications! {
                    server.reloadTimeline(for: complication)
                }
            }
        }
    }

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if (activationState.rawValue == 2){
            checkValue{(isResponse) -> Void in
                self.level = isResponse;
                let server=CLKComplicationServer.sharedInstance()
                for complication in server.activeComplications! {
                    server.reloadTimeline(for: complication)
                }
            }
        }
    }


    func checkValue(completionHandler : @escaping ((_ response : Int) -> Void))  {
        if WCSession.isSupported() {

            session = WCSession.default()

            session.sendMessage(["getValue": ""], replyHandler: { (dict) -> Void in

                completionHandler(round(dict["value"] as! Double * 100).toInt()!)
            }, errorHandler: { (error) -> Void in
                print("InterfaceController session error: \(error)")
                completionHandler(1111) //our error code
            })
        }
    }

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

    }

    func getTimelineEntries(for complication: CLKComplication, before date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        // Call the handler with the timeline entries prior to the given date
        handler(nil)
    }

    func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
        // Call the handler with the timeline entries after to the given date
        handler(nil)
    }

    // MARK: - Placeholder Templates

    func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
        // This method will be called once per supported complication, and the results will be cached
        handler(nil)
    }

}
4

0 に答える 0