[英]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. WatchKit
中的问题之一是从主应用程序与WatchKit
扩展进行通信。 Some of the ways to do by sending Darwin notifications or with MMWormhole
. 通过发送达尔文通知或使用
MMWormhole
进行操作的一些方法。
Could it be possible to do it by storing a reply block, sent by the extension to handleWatchKitExtensionRequest
, on a property? 是否可以通过在属性上存储扩展发送给
handleWatchKitExtensionRequest
的回复块来做到这一点? 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? 如果没有,为什么不可能呢?
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. 我会小心,尽管不要每次都
watchKitReply
属性并创建允许多个reply
对象的不同方法。
What we did in our Watch Extension / iOS App integration was to create a WatchKitRequest
set of requests. 我们在Watch Extension / iOS App集成中
WatchKitRequest
是创建一个WatchKitRequest
请求集。 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. 然后,将每个答复块转发到执行请求实现的每个
WatchKitRequest
子类,然后在完成后执行reply
块。 Essentially we're doing exactly what you suggested which is store the reply
block until you actually need to call it. 从本质上讲,我们正在按照您的建议进行操作,即存储
reply
块,直到您真正需要调用它为止。 We're just storing the reply
block a bit differently than you suggested. 我们存储的
reply
块与您建议的有所不同。
This approach worked extremely well for us. 这种方法对我们非常有效。 Hopefully this helps keep you moving in the right direction.
希望这有助于您朝正确的方向前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.