从 WidgetKit Complication 访问 iOS 应用程序/WatchOS 应用程序数据 (WatchOS 9)

Sim*_* C. 6 widgetkit apple-watch-complication swiftui watchos-9

我有一个应用程序,包括 Apple Watch 应用程序和 WidgetKit (iOS 16) 扩展。

\n

iOS 应用程序将数据发送WatchConnectivity到 Apple Watch 应用程序并在那里显示。它还使用 向 WidgetKit ( iOS 16-Lockscreen ) 扩展提供数据GroupUserDefaults。所有这些都可以正常工作\xe2\x9c\x85。

\n

我的问题:

\n

我现在正在尝试向WidgetKit complication extensionWatchOS 9)提供数据。我希望能够访问以前编写的UserDefaultsApple Watch 应用程序或共享的GroupUserDefaults. 但我无法访问其中的任何数据。我还检查了文档,但我找不到任何明确设计用于向并发症共享数据的方法。\nClockKit方法将不适用于WidgetKit并发症(https://developer.apple.com/documentation/widgetkit/converting- a-clockkit-应用程序)。

\n

是否有人设法将数据共享到 WidgetKit 复杂功能,而无需像 Apple 示例那样请求服务器 API?(https://developer.apple.com/documentation/widgetkit/adding_widgets_to_the_lock_screen_and_watch_faces

\n

Sim*_* C. 6

所以我可以回答我自己的问题:

应用程序组共享的 UserDefaults 似乎仅在设备本身上可用。因此,有必要在 Watch 应用程序的 WidgetKit 扩展以及 Apple Watch 应用程序上启用应用程序组功能。然后,Watch 应用程序可以将通过 WatchConnectivity 接收到的数据写入应用程序组共享的 UserDefaults,WidgetKit 扩展可以从中访问它。

因此,如果您遇到任何问题,请检查两个目标的功能以及配置文件和标识符(如果您手动/快速通道管理证书)。