[英]NSUserDefaults not storing values between app and custom keyboard in iOS 8
I'm trying to share data between my application and a custom keyboard extension. 我正在尝试在我的应用程序和自定义键盘扩展之间共享数据。 I've turned on App Groups in both the main application target and the custom keyboard target.
我在主应用程序目标和自定义键盘目标中打开了App Groups。 In my main application, I add an object with the following:
在我的主应用程序中,我添加了一个具有以下内容的对象:
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myapp"];
[userDefaults setObject:someObject forKey:@"KEY"];
Printing out [userDefaults dictionaryRepresentation] in the console reveals that this object has been saved, as does calling [userDefaults objectForKey:@"KEY"]. 在控制台中打印出[userDefaults dictionaryRepresentation]会显示该对象已被保存,调用[userDefaults objectForKey:@“KEY”]也是如此。
However, when I try to access this object in the custom keyboard extension: 但是,当我尝试在自定义键盘扩展中访问此对象时:
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myapp"];
NSString *value = [userDefaults objectForKey:@"KEY"];
The value is nil and a call to [userDefaults dictionaryRepresentation] does not reveal the entry that was saved above. 值为nil,对[userDefaults dictionaryRepresentation]的调用不会显示上面保存的条目。 I'm on Xcode 6 beta 3. Any ideas?
我正在使用Xcode 6 beta 3.有什么想法吗?
UPDATE Fixed in Xcode 6 beta 5 更新已在Xcode 6 beta 5中修复
A few probable solutions are: 一些可能的解决方案是:
Your app group is not setup correctly, or you are not using the correct group identifier with initWithSuiteName:
您的应用组未正确设置,或者您没有在
initWithSuiteName:
使用正确的组标识符initWithSuiteName:
You have not enabled network access for your keyboard. 您尚未为键盘启用网络访问权限。 This document states the following when you have network access disabled for your keyboard (default behavior):
当您为键盘禁用网络访问时, 此文档说明以下内容(默认行为):
No shared container with containing app
没有包含app的共享容器
It's a bug. 这是一个错误。
Set 组
RequestsOpenAccess = YES;
Settings: 设置:
NSUserDefaults * usrInfo = [[NSUserDefaults alloc] initWithSuiteName:@"myKeyboard"]; [usrInfo setObject:theme.icon forKey:@"themeName"]; // This is the new data; [usrInfo synchronize];
keyboardChange: keyboardChange:
NSUserDefaults * usrInfo = [[NSUserDefaults alloc] initWithSuiteName:@"myKeyboard"]; [usrInfo synchronize]; NSString * str = [usrInfo objectForKey:@"themeName"];
Then you can change the keyboard , for example ,change its background 然后你可以改变键盘,例如,改变它的背景
我认为您的套件名称必须从组开始,并与您制作的容器( 源 )匹配。
我发现它必须遵循这种格式:group.com.company.appname - 我有别的东西而且它不起作用。
Your group name (as set in Xcode) has to start with "group" (eg" " group.com.myName.MyApp " (I learned this when creating a group capability since the name field is already populated with "group".) and the NSUserDefaults suiteName has to start with another "group", so "group" is there twice, eg " group.group.com.myName.MyApp "). I learned this by looking at the app's Library/Preferences directory when it was running in the sim. Nothing would appear there unless I used this scheme. 您的组名(在Xcode中设置)必须以“group”开头(例如“” group.com.myName.MyApp “(我在创建组功能时学到了这一点,因为名称字段已经填充了”group“。)并且NSUserDefaults suiteName必须从另一个“组”开始,所以“group”有两次,例如“ group.group.com.myName.MyApp ”)。我通过查看应用程序的Library / Preferences目录来了解这一点。在sim中运行。除非我使用这个方案,否则什么都不会出现。
I still haven't been able to get the watch app to see the common group defaults file. 我仍然无法让手表应用程序看到公共组默认文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.