How to establish a communication channel between Apple Watch Extension/App and iOS App

浪子不回头ぞ 提交于 2019-12-04 12:29:43

问题


I'm exploring the WatchKit SDK.

When I have a WatchKit app, is it possible to set values (e.g. Text, settings) from the iPhone app on the WatchKit app? Can I call functions in the WatchKit app extension from the iPhone app?

If someone managed to do this, could he/she post an example? Thanks!


回答1:


You can use App Group and sharedDefaults to share data between the WatchApp/Extension and the App on the phone. see example: WatchKit SDK not retrieving data from NSUserDefaults

Study up on iOS8 Extension/App Groups/sharedDefaults and watchkit extension will make more sense.

This sample takes a UIImage from Shinobi chart, save it to defaults as image. Then extension picks up the image through defaults and shows it on watch

https://github.com/ChrisGrant/ChartWatch

This one uses multipeer connectivity to have watch talk to phone.

https://github.com/jsclayton/swatches

but this uses Bluetooth and I presume the Watch OS also communicates to the phone using bluetooth so not sure if they'll both be allowed.

We have no devices to test on yet so the /swatches app is just watch simuator talking to iphone simulator on same mac.

If youve ever done low level AV programming you know the app may run on the simulator but fail on the device because the simulator can cheat an use Mac OSX media layer. May be the same for bluetooth.

other samples

https://github.com/search?q=WKInterfaceController&type=Code




回答2:


There are several solutions: CoreData, NSKeyedUnarchiver and NSUserDefaults. With a common background for sharing the common data resource (Database, file or user default settings), this is enabling App Groups capabilities on both targets project properties.

In the following post is explained how to do it with default settings and you can also download the demo project.




回答3:


With watchOS2 now Apple supports Watch Connectivity Framework to pass information from watch extension to app and vice versa.

Taken from Apple's docs

Communicating with Your Companion iOS App

The Watch Connectivity framework lets you create a bidirectional communications channel between your WatchKit extension and your companion iOS app. Use this channel to coordinate activities between the two processes. For example, you might use this framework to push updated information from your iOS app to your WatchKit extension. The framework provides options for transferring data in the background or while both apps are active and replaces the existing openParentApplication:reply: method of the WKInterfaceController class.

For more information the classes of the Watch Connectivity framework, see Watch Connectivity Framework Reference.

Taken from Apple's Developers Library




回答4:


You can see this library https://github.com/mutualmobile/MMWormhole

It do Message passing between iOS apps and extensions.




回答5:


looks like the links with brain.clear are not pointing to the right destination for ShinobiChart example

https://github.com/ShinobiControls/ChartWatch



来源:https://stackoverflow.com/questions/27144114/how-to-establish-a-communication-channel-between-apple-watch-extension-app-and-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!