我想通过WCSession更新我的复杂功能,但注意似乎有效。我每10分钟更新一次并发症(我发现这是最大的)。但没有任何反应。任何人都可以在我的代码中看到问题。
我已经为更新实现了所需的委托。这是什么问题?非常感谢你!
导入ClockKit 导入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)
}
}