Communicate data between WatchOS & Today Extension widget

拟墨画扇 提交于 2019-12-11 04:44:16

问题


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

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