1

I'm hoping someone here may have some thoughts on Watch OS 2 connectivity that can point me in the right direction. Succinctly, I am trying to pass a set of data (from a custom class called FileData) to my WatchKit extension. When I run the WatchKit app, I can see that the WCSession is being activated, but the dataset never seems to get passed to the Watch (though if I change the data to a String and pass something simple like "hello", it does work properly);

TableViewController.swift (iOS side)

...
func sendToWatch(files: [FileData]) {
    let session = WCSession.defaultSession()
    let applicationData = ["myFiles":[FileData](files)]
    session.sendMessage(applicationData, replyHandler: { reply in
        print("Got reply: \(reply)")
    }, errorHandler: { error in
        print("error: \(error)")
    })
}
...

InterfaceController.swift (WatchKit extension)

...
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
    let files = message["myFiles"] as! [FileData]
    print("Got a message")
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            print(files)
        })

    reloadTable()
}
...

Am I wrong in assuming that I can pass a custom class via Watch Connectivity? Or have I done something wrong here?

Thank you!

ZbadhabitZ
  • 2,753
  • 1
  • 25
  • 45

1 Answers1

1

Yes, that is an incorrect assumption. The WCSession sendMessage documentation states that the dictionary may only contain property list types which are basic types such as strings, integers, floats, data, etc. So to send your content, either convert the object to a dictionary of key-value pairs or use the less recommended approach of using NSKeyedArchiver to convert your object directly to data.

ccjensen
  • 4,578
  • 2
  • 23
  • 25
  • That makes sense. Thank you for clarifying! – ZbadhabitZ Aug 12 '15 at 11:29
  • Cool! The sendMessage error handler would also have been returned an error indicating that the payload contained invalid types – ccjensen Aug 12 '15 at 11:42
  • The strange part is that I am actually able to send the data (using `let msg = ["myFiles":String(self.files)]`) though I have a feeling that I'm just forcing my "files" dictionary to be a string and not a custom class anymore. It's actually sending to WatchKit, though I'm having issues with making use of that data on WatchKit, which is a whole other problem. Thanks again! – ZbadhabitZ Aug 12 '15 at 11:44
  • I'd imagine when you create a string from the "files" dictionary it is calling the description method on it which just gives you a simplified string version of the dictionary (similar to what'd you'd get if you logged the dictionary) – ccjensen Aug 12 '15 at 13:03