问题
Standard set up for Watch OS > 2.
WCSessionDelegate
used to coordinte data between main application and Watch.
An App Group "group.***********.TodayExtensionWidget"
used to coordinate data between main application and its Today Extension widget via UserDefaults(suiteName: "group.***********.TodayExtensionWidget")
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.
Is there a best practice to communicate between a watch app and a Today Extension widget?
回答1:
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.
As you already experienced, WatchConnectivity
cannot be used in a Today extension
, so that is out of the picture as well.
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.
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
.
来源:https://stackoverflow.com/questions/45752905/communicate-data-between-watchos-today-extension-widget