I would need to return an array of dictionaries when polling the main app from the WatchKit extension. Yet it seems the only plain version of it just uses Dictionaries. So I tried packing the array in an Archive and send it instead through:
public func sendMessageData(data: NSData, replyHandler: ((NSData) -> Void)?, errorHandler: ((NSError) -> Void)?)
and receiving it by:
func session(session: WCSession, didReceiveMessageData messageData: NSData, replyHandler: (NSData) -> Void)
Yet, differently from the dictionary version, the latter function gets not called. What might be wrong? The version at: https://developer.apple.com/library/watchos/documentation/WatchConnectivity/Reference/WCSessionDelegate_protocol/index.html#//apple_ref/occ/intfm/WCSessionDelegate/session:didReceiveMessageData:replyHandler: reports a different format that does not compile, though. Those are the two functions:
// watckit extension
func poll() {
if WCSession.defaultSession().reachable{
let requestValue = ["command": "buspoll"]
let data = NSKeyedArchiver.archivedDataWithRootObject(requestValue)
if let safeSession = session{
if safeSession.reachable {
safeSession.sendMessageData(data, replyHandler: {[unowned self] (resultData) -> Void in
let reply = NSKeyedUnarchiver.unarchiveObjectWithData(resultData) as! [Dictionary<String, String>]
self.myTable.setNumberOfRows(reply.count, withRowType: "WKBusRow")
for index in 0..<reply.count {
let row=self.myTable.rowControllerAtIndex(index) as! WKBusArrivalRow
let busDestination = reply[index]["destination"]
let timeLocation = reply[index]["time"]
row.bus.setText(busDestination)
print("destination=\(busDestination)")
row.location.setText(timeLocation)
print("time=\(timeLocation)")
}
}, errorHandler: { (error) -> Void in
print("error: \(error)")
})
}
}
}
}
//MainApp
public func session(session: WCSession,
didReceiveMessageData messageData: NSData,
replyHandler: (NSData) -> Void) {
var data=NSData()
let message = NSKeyedUnarchiver.unarchiveObjectWithData(messageData) as! Dictionary<String, String>
var replyValues = [Dictionary<String, String>]()
print("ricevuto " + message["command"]!)
switch message["command"]!{
case "buspoll":
for index in 0..<busData.count {
replyValues[index]["destination"] = String(format: "%@: %@", busData[index], destionationsData[index])
replyValues[index]["time"] = String(format: "%@ min. at %@", minutesData[index], locationData[index])
}
data = NSKeyedArchiver.archivedDataWithRootObject(replyValues)
default:
break
}
replyHandler(data)
}