如何在Apple Watch Extension / App和iOS App之间建立通信渠道

时间:2022-08-26 18:53:33

I'm exploring the WatchKit SDK.

我正在探索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?

当我有WatchKit应用程序时,是否可以从WatchKit应用程序上的iPhone应用程序设置值(例如文本,设置)?我可以从iPhone应用程序调用WatchKit应用程序扩展中的函数吗?

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

如果有人设法做到这一点,他/她可以发布一个例子吗?谢谢!

5 个解决方案

#1


9  

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

您可以使用App Group和sharedDefaults在手机上的WatchApp /分机和应用程序之间共享数据。参见示例:WatchKit SDK无法从NSUserDefaults检索数据

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

研究iOS8 Extension / App Groups / sharedDefaults和watchkit扩展将更有意义。

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

此示例从Shinobi图表中获取UIImage,将其保存为默认值为image。然后,扩展程序会通过默认值选择图像,并在手表上显示

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.

但这使用蓝牙,我认为Watch OS也使用蓝牙与手机通信,因此不确定它们是否都被允许。

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

我们还没有可以测试的设备,所以/ swatches应用程序只是在同一台Mac上观看模拟器与iphone模拟器通话。

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.

如果您曾经进行过低级别的AV编程,您就会知道应用程序可能在模拟器上运行但在设备上失败,因为模拟器可能会欺骗使用Mac OSX媒体层。蓝牙也许是一样的。

other samples

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

#2


3  

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.

有几种解决方案:CoreData,NSKeyedUnarchiver和NSUserDefaults。通过共享公共数据资源(数据库,文件或用户默认设置)的通用背景,这将在两个目标项目属性上启用应用程序组功能。

如何在Apple Watch Extension / App和iOS App之间建立通信渠道

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

在下面的文章中解释了如何使用默认设置执行此操作,您还可以下载演示项目。

#3


3  

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

通过watchOS2,Apple现在支持Watch Connectivity Framework将信息从手表扩展传递到应用程序,反之亦然。

Taken from Apple's docs

摘自Apple的文档

Communicating with Your Companion iOS App

与您的Companion iOS应用程序通信

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.

Watch Connectivity框架可让您在WatchKit扩展程序和配套iOS应用程序之间创建双向通信渠道。使用此通道来协调两个进程之间的活动。例如,您可以使用此框架将更新的信息从iOS应用推送到WatchKit扩展。该框架提供了在后台或两个应用程序都处于活动状态时传输数据的选项,并替换了WKInterfaceController类的现有openParentApplication:reply:方法。

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

有关Watch Connectivity框架的更多信息,请参阅Watch Connectivity Framework Reference。

Taken from Apple's Developers Library

摘自Apple的开发者库

#4


2  

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

你可以看到这个库https://github.com/mutualmobile/MMWormhole

It do Message passing between iOS apps and extensions.

它在iOS应用和扩展之间传递消息。

#5


1  

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

看起来像brain.clear的链接没有指向ShinobiChart示例的正确目的地

https://github.com/ShinobiControls/ChartWatch

#1


9  

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

您可以使用App Group和sharedDefaults在手机上的WatchApp /分机和应用程序之间共享数据。参见示例:WatchKit SDK无法从NSUserDefaults检索数据

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

研究iOS8 Extension / App Groups / sharedDefaults和watchkit扩展将更有意义。

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

此示例从Shinobi图表中获取UIImage,将其保存为默认值为image。然后,扩展程序会通过默认值选择图像,并在手表上显示

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.

但这使用蓝牙,我认为Watch OS也使用蓝牙与手机通信,因此不确定它们是否都被允许。

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

我们还没有可以测试的设备,所以/ swatches应用程序只是在同一台Mac上观看模拟器与iphone模拟器通话。

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.

如果您曾经进行过低级别的AV编程,您就会知道应用程序可能在模拟器上运行但在设备上失败,因为模拟器可能会欺骗使用Mac OSX媒体层。蓝牙也许是一样的。

other samples

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

#2


3  

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.

有几种解决方案:CoreData,NSKeyedUnarchiver和NSUserDefaults。通过共享公共数据资源(数据库,文件或用户默认设置)的通用背景,这将在两个目标项目属性上启用应用程序组功能。

如何在Apple Watch Extension / App和iOS App之间建立通信渠道

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

在下面的文章中解释了如何使用默认设置执行此操作,您还可以下载演示项目。

#3


3  

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

通过watchOS2,Apple现在支持Watch Connectivity Framework将信息从手表扩展传递到应用程序,反之亦然。

Taken from Apple's docs

摘自Apple的文档

Communicating with Your Companion iOS App

与您的Companion iOS应用程序通信

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.

Watch Connectivity框架可让您在WatchKit扩展程序和配套iOS应用程序之间创建双向通信渠道。使用此通道来协调两个进程之间的活动。例如,您可以使用此框架将更新的信息从iOS应用推送到WatchKit扩展。该框架提供了在后台或两个应用程序都处于活动状态时传输数据的选项,并替换了WKInterfaceController类的现有openParentApplication:reply:方法。

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

有关Watch Connectivity框架的更多信息,请参阅Watch Connectivity Framework Reference。

Taken from Apple's Developers Library

摘自Apple的开发者库

#4


2  

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

你可以看到这个库https://github.com/mutualmobile/MMWormhole

It do Message passing between iOS apps and extensions.

它在iOS应用和扩展之间传递消息。

#5


1  

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

看起来像brain.clear的链接没有指向ShinobiChart示例的正确目的地

https://github.com/ShinobiControls/ChartWatch