Reputation: 153
I'm new to Swift and started my first app.
I'm trying to transfer data from the Apple watch to the iPhone using updateApplicationContext, but only get an error:
[WCSession updateApplicationContext:error:]_block_invoke failed due to WCErrorCodePayloadUnsupportedTypes
This is the code in my WatchKit Extension:
var transferData = [JumpData]()
func transferDataFunc() {
let applicationDict = ["data":self.transferData]
do {
try self.session?.updateApplicationContext(applicationDict)
} catch {
print("error")
}
}
These are the object structure I want to send:
class AltiLogObject {
let altitude : Float
let date : Date
init (altitude: Float) {
self.altitude = altitude
self.date = Date.init()
}
init (altitude: Float, date : Date) {
self.altitude = altitude
self.date = date
}
}
class JumpData {
let date : Date
let altitudes : [AltiLogObject]
init(altitudes: [AltiLogObject]) {
self.altitudes = altitudes
self.date = Date.init()
}
init(altitudes: [AltiLogObject], date : Date) {
self.date = date
self.altitudes = altitudes
}
}
To have it complete, the code of the receiver function:
private func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
let transferData = applicationContext["data"] as! [JumpData]
//Use this to update the UI instantaneously (otherwise, takes a little while)
DispatchQueue.main.async() {
self.jumpDatas += transferData
}
}
Any hints are welcome, as I'm trying to get it running for over a week now.
Upvotes: 3
Views: 624
Reputation: 450
You can only send property list values through updateApplicationContext() - the documentation states this but it isn’t clear at all.
The type of objects that you can send can be found at https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html
Obviously your custom object types aren’t supported, so what you need to do is to break the properties down into the constituent parts (e.g your altitude and date properties), send them individually in the applicationContext dictionary passed into updateApplicationContext() and then reconstruct the objects on the receiving end.
It’s a real pain of course, but that’s the way this all works.
HTH
Upvotes: 3