Search code examples
iosswiftmacosfacetime

How can I correctly decode SharePlay data?


I set up SharePlay for a multiplatform iOS/macOS app, I can even start and join the session and send data between the two devices, but whenever data is received, I get the following error:

SharePlayTutorialMac[33577:704067] [Default] messageStream:108 Explanation: Decoding message from data Error: Swift.DecodingError.valueNotFound(Any, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: “message”, intValue: nil), CodingKeys(stringValue: “t”, intValue: nil)], debugDescription: “Decoder for value of Swift.Array<SharePlayTutorialMac.SharePlayModel>.self not found.”, underlyingError: nil))

I do not even have any values of message or t as mentioned in the error - does anyone know why the decoding fails?


Solution

  • After checking the error message in detail, I saw that the model's type was somehow referenced in an absolute way like SharePlayTutorialMac.SharePlayModel instead of just SharePlayModel.

    I found out that the problem was due to different product names since I first created an iOS target and added the macOS target afterwards, which led to a product name referenced as $(TARGET_NAME). Therefore, you just have to use the same product name for both targets! For multiplatform apps, this is done by default.

    I also stumbled upon other problems and therefore, wrote a whole tutorial for the SharePlay setup - you can find a more detailed description of the product name fix here.