0

Please forgive the "noob"ness of my inquiry, but I'm hoping someone can help me wrap my head around this concept.

I have an iOS app (Swift) that is parsing JSON data from a remote server. It gets back some details of 3 servers in my organization. I have a UITableView set up the name and small bits of data in cells, and tapping on a cell will take me to another UITableView that shows more detail of the parsed data. It's working well.

I am attempting to migrate this idea to a WatchKit app, wherein I hope to have a table that will populate its labels based on a specific piece of that JSON data. Ideally, a user could tap on a cell of this table and get the detailed JSON data I parsed back in the iOS app.

Where I'm hitting a roadblock is trying to understand how WatchKit can pass the JSON data (I realize the need to use app containers and a User Default or File Coordinator), and allow the user to select the proper cell and receive the proper JSON data.

Without asking for a blatant answer, could someone provide some guidance or resource on who to perform this task? Or is selecting a cell via WatchKit not going to allow for an identifier that would populate the right JSON data?

Thank you in advance!

ZbadhabitZ
  • 2,753
  • 1
  • 25
  • 45

1 Answers1

0

You can request information from your iPhone app by using the openParentApplication:reply: class method on WKInterfaceController (here).

Using this method, your app will be launched in the background (if it's not already running), and you can pass your JSON data in the reply.

Alternately, you could store your data in an app group shared container where both the iPhone and Watch Extension can access it directly.

Mike Swanson
  • 3,337
  • 1
  • 17
  • 15
  • Thanks so much, Mike! That was a great response and very helpful in understanding this mindset. – ZbadhabitZ Apr 24 '15 at 15:38
  • Out of curiosity, would you happen to have a suggestion of any resources to learn how one would pass/set JSON datasets to the reply in openParentApplication? I've seen some great resources/tutorial on how to pass static data or single variables in the reply, but have yet to see how to set a dictionary with a gathered JSON dataset to the reply. As always, thanks! – ZbadhabitZ Apr 24 '15 at 15:49
  • I'd take a look at `NSJSONSerialization`. One of these answers should help: http://stackoverflow.com/questions/6368867/generate-json-string-from-nsdictionary – Mike Swanson Apr 24 '15 at 16:52
  • Thanks, @Mike! I'm getting closer now. I'm using [SwiftyJson](https://github.com/SwiftyJSON/SwiftyJSON) to parse my JSON data. In a broader sense, I have a custom set string (as a test) successfully being passed to the app group shared container using NSUserDefaults. When I try to pass the whole set of JSON data, though, I receive the error `Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')`. Is this where your `NSJSONSerialization` comes in, or do I need to find a way to conform the JSON data to a property list format? – ZbadhabitZ Apr 24 '15 at 17:18
  • Whatever data you pass, it'll have to be a format that can be serialized to a property list. This includes things like arrays, dictionaries, strings, etc. Sounds like you might be including something that can't be serialized. – Mike Swanson Apr 24 '15 at 20:41