简体   繁体   English

从 WidgetKit Complication (WatchOS 9) 访问 iOS App / WatchOS App 数据

[英]Access iOS App / WatchOS App data from WidgetKit Complication (WatchOS 9)

I have an app inlcuding an Apple Watch app and a WidgetKit (iOS 16) extension.我有一个包含 Apple Watch 应用和 WidgetKit (iOS 16) 扩展的应用。

The iOS app sends data via WatchConnectivity to the Apple Watch app and displays it there. iOS 应用程序通过WatchConnectivity将数据发送到 Apple Watch 应用程序并在那里显示。 Also it provides data to a WidgetKit ( iOS 16 - Lockscreen ) extension using GroupUserDefaults .它还使用GroupUserDefaults向 WidgetKit ( iOS 16-Lockscreen ) 扩展提供数据。 All of this works without any issues ✅.所有这些都可以正常工作✅。

My issue:我的问题:

I'm now trying to supply data to a WidgetKit complication extension ( WatchOS 9 ).我现在正在尝试向WidgetKit complication extensionWatchOS 9 )提供数据。 I expected to be able to either access the previously written UserDefaults of the Apple Watch app or the shared GroupUserDefaults .我希望能够访问 Apple Watch 应用程序的先前编写的UserDefaults或共享的GroupUserDefaults But I can't access any of it's data.但我无法访问它的任何数据。 I also checked the documentation, but I can't find any methods explicitely designed to share data to the complications.我还检查了文档,但我找不到任何明确设计用于共享数据的方法。 ClockKit methods will not be available for WidgetKit complications ( https://developer.apple.com/documentation/widgetkit/converting-a-clockkit-app ). ClockKit方法将不适用于WidgetKit并发症 ( https://developer.apple.com/documentation/widgetkit/converting-a-clockkit-app )。

Did anybody manage to share data to a WidgetKit complication without requesting a Server API like in the Apple example?是否有人设法在不请求服务器 API 的情况下将数据共享到 WidgetKit 复杂功能,就像 Apple 示例中那样? ( https://developer.apple.com/documentation/widgetkit/adding_widgets_to_the_lock_screen_and_watch_faces ) https://developer.apple.com/documentation/widgetkit/adding_widgets_to_the_lock_screen_and_watch_faces

So I can answer my own question:所以我可以回答我自己的问题:

App Group shared UserDefaults are available only on the device itself it seems. App Group 共享的 UserDefaults 似乎仅在设备本身上可用。 Therefore it's necessary to enable the App Group capability on the WidgetKit extension of the Watch app as well as on the Apple Watch app .因此,有必要在 Watch 应用程序的 WidgetKit 扩展以及 Apple Watch 应用程序上启用 App Group 功能 The Watch App can then write the data recieved through WatchConnectivity to the App Group shared UserDefaults from where the WidgetKit extension can access it.然后,Watch App 可以将通过 WatchConnectivity 接收的数据写入 App Group 共享的 UserDefaults 中,WidgetKit 扩展可以从中访问它。

So if you encounter any issues, check the capabilites of both targets and also profiles and identifiers , if you manage certificates manually/fastlane.因此,如果您遇到任何问题,请检查目标以及配置文件和标识符的功能,如果您手动/快速管理证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM