[英]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.