简体   繁体   English

如何创建/使用共享应用程序组容器作为包含应用程序及其在ios中的扩展名之间的缓存存储

[英]How to create/use a shared app group container as cache storage between containing app and it's extension in ios

Data sharing between app and its extension occurs through common AppGroup. 应用程序及其扩展之间的数据共享通过常见的AppGroup进行。 I can use shared app group container as persistant data/file storage. 我可以使用共享应用程序组容器作为持久数据/文件存储。 How can I create/use shared app group container as cache storage between a containing app and its extension? 如何在包含应用程序及其扩展程序之间创建/使用共享应用程序组容器作为缓存存储? Is this even possible? 这甚至可能吗?

Yes, you are on the right track. 是的,你走在正确的轨道上。 This is possible. 这个有可能。

In the Capabilities tab of your main app's target and your WatchKit Extension's target, add an app group, eg, with the name group.myWatchDefaults . 在主应用程序目标的Capabilities选项卡和WatchKit Extension的目标中,添加一个应用程序组,例如名称为group.myWatchDefaults

Then you can access the defaults like this: 然后你可以访问这样的默认值:

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.myWatchDefaults"];

To add data: 要添加数据:

[defaults setObject:[NSNumber numberWithBool:true] forKey:@"myBoolVariable"];

To retrieve data: 要检索数据:

 bool watchAppOpenedBefore = [[defaults objectForKey:@"myBoolVariable"] boolValue];

Make sure that you use synchronize after changing the defaults: 确保在更改默认值后使用synchronize

[defaults synchronize];

Sometimes synchronizing NSUserDefaults between a Watch app and its parent app using App Groups doesn't work when using Xcode's simulators. 有时,使用应用程序组在Watch应用程序与其父应用程序之间同步NSUserDefaults时,使用Xcode的模拟器时无效。 However, it works on the actual devices (iPhone and Apple Watch). 但是,它适用于实际设备(iPhone和Apple Watch)。 Therefore, this seems to be a bug in the simulator. 因此,这似乎是模拟器中的一个错误。

暂无
暂无

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

相关问题 如何在iOS App包含扩展和扩展(不是主机应用程序)之间进行通信 - How to communicate between iOS App Containing Extension and Extension (not Host App) 无法从iOS 10中的App组共享容器中获取扩展程序中的图像 - Not able to fetch images in extension app from App group shared container in iOS 10 iOS 11+ 如何将现有核心数据迁移到共享应用组以用于扩展? - iOS 11+ How to migrate existing Core Data to Shared App Group for use in extension? IOS应用程序和手表扩展之间的共享文件 - Shared files between IOS app and watch extension iOS Couchbase:应用程序和应用程序扩展之间共享的CBL管理器 - iOS Couchbase: Shared CBL manager between app and app extension 调试 iOS 应用时如何查看 App Group Shared Container 目录的内容? - How can I view the contents of the App Group Shared Container directory when debugging an iOS app? iOS8 无法在应用组共享容器位置下创建文件夹/文件 - iOS8 Unable to create a folder/file under app group shared container location 在应用扩展程序的共享容器中找不到文件 - Cannot find file in app extension's shared container 如何使用应用程序扩展中的 IPC 将信息异步发送到 iOS 上的包含应用程序? - How can I use IPC from an app extension to asynchronously send information to the containing app on iOS? 发布NSUserDefaults在iOS键盘扩展和包含应用之间进行更改吗? - Post NSUserDefaults changes between iOS Keyboard Extension and Containing app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM