WatchOS & 之间的数据通信今天扩展小部件 [英] Communicate data between WatchOS & Today Extension widget
问题描述
Watch OS 的标准设置 > 2.WCSessionDelegate
用于协调主应用程序和 Watch 之间的数据.
Standard set up for Watch OS > 2.
WCSessionDelegate
used to coordinte data between main application and Watch.
一个应用程序组 "group.************.TodayExtensionWidget"
用于通过 UserDefaults(suiteName:"group.***********.TodayExtensionWidget")
An App Group "group.***********.TodayExtensionWidget"
used to coordinate data between main application and its Today Extension widget via UserDefaults(suiteName: "group.***********.TodayExtensionWidget")
当我从手表中进行更改时,它会与主应用程序传达该更改.然后主应用程序(一旦启动)将其传达给 Today Extension.我想做的是将手表应用中的更改传达给 Today 扩展,而无需先启动主应用.
When I make a change from the watch it communicates that change with the main application. Then the main application (once launched) communicates this on to the Today Extension. What I would like to do is communicate the change in the watch app to the Today extension without needing to launch the main app first.
是否有在手表应用和 Today Extension 小部件之间进行通信的最佳做法?
Is there a best practice to communicate between a watch app and a Today Extension widget?
推荐答案
目前无法使用任何内置框架来实现这一点.由于watchOS2
的引入,WatchKit
应用被认为是独立的应用,而不仅仅是iOS
应用的扩展,因此你不能使用AppGroups
在两者之间共享数据,尤其是不能使用它在 WatchKit
应用程序和 iOS
扩展之间共享数据.
At the moment there is no way to achieve this using any built-in frameworks. Since the introduction of watchOS2
, WatchKit
apps are considered to be independent apps and not just an extension of the iOS
app, hence you cannot use AppGroups
to share data between the two and especially cannot use it to share data between the WatchKit
app and an iOS
extension.
正如您已经体验过的,WatchConnectivity
不能用于 Today 扩展
,因此也不适用.
As you already experienced, WatchConnectivity
cannot be used in a Today extension
, so that is out of the picture as well.
您唯一的选择是次优选择,因为您需要将数据从 watchOS
应用上传到服务器,并在 iOS Today 扩展程序
中下载.这当然会产生不必要的数据使用,但使用 Apple 提供的当前框架,您无法离线共享数据.
Your only option is a suboptimal one, since you will need to upload your data to a server from your watchOS
app and download it in your iOS Today extension
. This of course generates unnecessary data usage, but with the current frameworks provided by Apple you cannot share the data offline.
但是,如果您等到 watchOS4
发布(或尝试测试版),您或许可以利用即将可用的 CoreBluetooth
框架watchOS4
中的 WatchKit
使用 BLE<在
WatchKit
应用程序和 iOS
扩展之间进行离线通信/代码>.我不是 100% 确定 CoreBluetooth
是否可以在 iOS 扩展
中使用,但是如果您可以使用 watchOS4 beta
,您应该尝试一下.
However, if you wait until watchOS4
is released (or give the beta version a try), you might be able to leverage the CoreBluetooth
framework, which is becoming available for WatchKit
in watchOS4
to communicate between a WatchKit
app and an iOS
extension offline, using BLE
. I am not 100% sure if CoreBluetooth
can be used in iOS Extensions
, but you should give this a try if you can use watchOS4 beta
.
这篇关于WatchOS & 之间的数据通信今天扩展小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!