How to establish a communication channel between A

2019-03-20 11:31发布

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!

5条回答
叼着烟拽天下
2楼-- · 2019-03-20 12:03

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

查看更多
小情绪 Triste *
3楼-- · 2019-03-20 12:10

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

It do Message passing between iOS apps and extensions.

查看更多
Animai°情兽
4楼-- · 2019-03-20 12:12

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.

enter image description here

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

查看更多
SAY GOODBYE
5楼-- · 2019-03-20 12:25

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

查看更多
Animai°情兽
6楼-- · 2019-03-20 12:26

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

https://github.com/ShinobiControls/ChartWatch

查看更多
登录 后发表回答