![](/img/trans.png)
[英]How to communicate between iOS App Containing Extension and Extension (not Host App)
[英]How to create/use a shared app group container as cache storage between containing app and it's extension in ios
应用程序及其扩展之间的数据共享通过常见的AppGroup进行。 我可以使用共享应用程序组容器作为持久数据/文件存储。 如何在包含应用程序及其扩展程序之间创建/使用共享应用程序组容器作为缓存存储? 这甚至可能吗?
是的,你走在正确的轨道上。 这个有可能。
在主应用程序目标的Capabilities
选项卡和WatchKit Extension的目标中,添加一个应用程序组,例如名称为group.myWatchDefaults
。
然后你可以访问这样的默认值:
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.myWatchDefaults"];
要添加数据:
[defaults setObject:[NSNumber numberWithBool:true] forKey:@"myBoolVariable"];
要检索数据:
bool watchAppOpenedBefore = [[defaults objectForKey:@"myBoolVariable"] boolValue];
确保在更改默认值后使用synchronize
:
[defaults synchronize];
有时,使用应用程序组在Watch应用程序与其父应用程序之间同步NSUserDefaults时,使用Xcode的模拟器时无效。 但是,它适用于实际设备(iPhone和Apple Watch)。 因此,这似乎是模拟器中的一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.