最佳答案
我得到一个错误,我的类不符合 NSObjectProtocol,我不知道这意味着什么。我已经实现了来自 WCSessionCommittee 的所有函数,所以这不是问题所在。有人知道问题出在哪吗?谢谢!
import Foundation
import WatchConnectivity
class BatteryLevel: WCSessionDelegate {
var session: WCSession? {
didSet {
if let session = session {
session.delegate = self
session.activate()
}
}
}
var batteryStatus = 0.0;
func getBatteryLevel(){
if WCSession.isSupported() {
// 2
session = WCSession.default()
// 3
session!.sendMessage(["getBatteryLevel": ""], replyHandler: { (response) -> Void in
if (response["batteryLevel"] as? String) != nil {
self.batteryStatus = (response["batteryLevel"] as? Double)! * 100
}
}, errorHandler: { (error) -> Void in
// 6
print(error)
})
}}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
}
}