0

Tell me, is it possible in the Watch Connectivity framework to transfer the following types of objects from the iOS application to WatchOS via the session.sendMessage method? If this is possible, then how?

Types of objects to send:

  1. MyCustomClass
  2. [MyCustomClass]
  3. Result < MyCustomClass > (Realm)

Thank you for the answers!

Lion Gurman
  • 161
  • 1
  • 2
  • 9

2 Answers2

2

The documentation for WCSession.sendMessage(_:replyHandler:errorHandler:) says:

message

A dictionary of property list values that you want to send. You define the contents of the dictionary that your counterpart supports. This parameter must not be nil.

Property list types are limited to dictionaries, arrays, strings, numbers, dates, binary data and boolean values. The types you list are not property list types, so you'd need to serialize them in some way before you could send them using sendMessage(_:replyHandler:errorHandler:). See Apple's documentation on Object archiving for information about how to serialize your own data types.

bdash
  • 18,110
  • 1
  • 59
  • 91
  • Can you give more detail or an example? – Lion Gurman Apr 03 '17 at 19:49
  • More detail on what, exactly? – bdash Apr 03 '17 at 19:56
  • How to serialize an object? – Lion Gurman Apr 03 '17 at 20:02
  • [Apple's documentation on Object archiving](https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Archiving.html#//apple_ref/doc/uid/TP40008195-CH1-SW1), linked to in my answer, describes how Apple's object serialization system works. Alternatively, you could use any number of frameworks that support serializing to JSON, since a JSON string would be a property list type that could be sent via `WCSession`. – bdash Apr 03 '17 at 20:05
0

You have two options: (1) Pass the data using the PropertyList method as described by bdash or, (2) by passing the data in the sendMessage(_:replyHandler:errorHandler:) method by building the message variable as a dictionary of type [String:Any] and then handling the response in the Watch Extension's didReceiveMessage method.

For an example of this approach, view here: https://github.com/markfilter/iOS_WatchConnectivity_Tutorial

Mark Filter
  • 353
  • 5
  • 8