how to pass some message or string from iphone dev

2019-08-04 12:50发布

问题:

I would like to know how a particular message or string can be passed from my iPhone device to the paired iWatch. I would really like to know how this is handled from development point of view. Is there any particular WKInterfaceController method or API which passes the info from iPhone to iWatch?

I have read on developer forum that openParentApplication:reply: notifies the iPhone application from iWatch and application:handleWatchKitExtensionRequest:reply handles that particular notification request on iPhone. But I want the reverse.

Lets say, I have an iPhone application storyboard scene with a UITextField and UIButton. User enters "Hello !" on text field and clicks on button. Now I have a watch kit extension within the same project with an iwatch storyboard. This storyboard scene has an "WKInterfaceLabel". I would like to have my iPhone controller interact with WKInterfaceController so that the message entered on iphone stroyboard ("Hello !" in this case) can be passed to the iWatch controller and displayed on the iWatch.

回答1:

In order to communicate from iPhone to your watch, you need to use app groups, and darwin notifications. You can use MMWormhole (available here) to do this.



回答2:

MMWormhole will do the required work. It uses darwin notification center to communicate between app and its extensions.

Simply install it using CocoaPods, or add MMWormhole.h and MMWormhole.m into your project.

Hope this helped.