Search code examples
iosobjective-cswiftwatchkitapple-watch

How to communicate between an iOS app and the WatchKit extension by storing the "reply" block?


One of the problems in WatchKit is communicating from the main app to the WatchKit extension. Some of the ways to do by sending Darwin notifications or with MMWormhole.

Could it be possible to do it by storing a reply block, sent by the extension to handleWatchKitExtensionRequest, on a property? Something like:

@property (atomic,copy) void (^watchKitReply)(NSDictionary *);

Then, when the app wants to send something to the extension, it uses this readily available block.

If the extension wants to communicate with the main app it will send a new reply block, that will be stored on the property. The old one will be used and discarded.

If not, why this isn't possible?


Solution

  • You can definitely do this. I would be careful though not to stomp your watchKitReply property each time and create a different way to allow multiple reply objects.

    What we did in our Watch Extension / iOS App integration was to create a WatchKitRequest set of requests. Each reply block is then forwarded to each WatchKitRequest subclass where the request implementation is executed, then the reply block is executed when it is complete. Essentially we're doing exactly what you suggested which is store the reply block until you actually need to call it. We're just storing the reply block a bit differently than you suggested.

    This approach worked extremely well for us. Hopefully this helps keep you moving in the right direction.