简体   繁体   中英

Sending a dictionary from iOS app to WatchKit - watchOS2

My goal is to send a dictionary to the watchKit from iOS app prior to the watchKit's app launch. I'm using interactive messaging (sendMessage) to quickly transfer the dictionary.

The issue is - dictionary is created inside the MainViewController . If i declare the WCSession and activate it inside the MainViewController i can transfer the data to the watchKit on the simulator without any problem. But when i test the process on a real device - the iOS app never gets called.

Waking the app in the background is done by declaring and activating the WCSession inside the AppDelegate , but there's another blocker - i cannot create the dictionary - because multiple variables for its creation are declared inside the MainViewController .

I tried a third approach - wrapping the WCSession inside a singleton ( suggested by Natasha the robot). The only drawback of this framework is that the Interactive messaging never works and wasn't ever tested by Natasha herself.

So i'm confused - what do i do to send the dictionary to the watchKit?

Thanks for any insights

You need to figure out a way to get the dictionary created outside of MainViewController . Perhaps you can write a class method in the controller that creates and returns the dictionary so that it can be used from both AppDelegate and MainViewController .

You should use a data store to hold your dictionary, then have it create its data based on the variables passed to it by the main view controller.

Once that occurs, you can use the WCSession manager to transfer the data store's dictionary.

I know Natasha covers these aspects in her tutorial. If you have a specific question as to how to do that, you'd really need to post code showing what you tried, along with a description of what's not working.

If the watch asks for data, but it has not been created yet, you need to return a "No data yet" reply so the watch can display a message telling the user to open the app and set the view controller's variables used for creating the data.

It really is better to separate and encapsulate responsibilities into these different components. The view controller shouldn't need to contain any code related to creating or transferring the dictionary.

Having said all that...

I cannot create the dictionary - because multiple variables for its creation are declared inside the MainViewController

This really sounds like an XY problem . You've been focused on the problem of "sending" this dictionary of large arrays that you have to create, when there's likely an easier way to accomplish what you're actually trying to do with this large dictionary in the first place.

For one, I'd wonder why you're sending that huge computed data set to the watch for it to do something with, instead of also handling that computation on the phone side, then sending a very small set of "results".

Perhaps you should describe the real Y problem you want to solve on the watch, instead of asking us for an X solution which may end up being unnecessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM