简体   繁体   English

如何通过存储“回复”块在iOS应用和WatchKit扩展之间进行通信?

[英]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.

相关问题 在iOS App和WatchKit Extension之间共享Plist - Share Plist between iOS App and WatchKit Extension 如何在iOS App包含扩展和扩展(不是主机应用程序)之间进行通信 - How to communicate between iOS App Containing Extension and Extension (not Host App) iOS WatchKit - 如何确定您的代码是在手表扩展程序还是应用程序中运行 - iOS WatchKit - how to determine if your code is running in watch extension or the app WatchKit扩展:如何从主机iOS应用读取数据? - WatchKit extension: how to read data from host iOS app? 如何从父级ios应用程序调用watchkit扩展中的方法 - How to call the method in watchkit extension from parent ios app 如何在WatchKit扩展和iPhone应用程序之间共享钥匙串数据 - How to share keychain data between a WatchKit extension and an iPhone app 具有WatchKit扩展名的父级应用程序是否为iOS 8及更高版本? - Does parent app with WatchKit extension be iOS 8 and later? 通过WatchKit扩展检测iOS应用是否在前台 - Detect if iOS app is in foreground from WatchKit extension 如何在以下用例的通知扩展和应用程序之间进行通信? - How to communicate between Notification Extension and app for below use case? 我如何在Watchkit中的GlanceController和InterfaceController之间进行通信? - How do I communicate between GlanceController and InterfaceController in Watchkit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM