简体   繁体   English

NSUserDefaults不在iOS 8中的应用程序和自定义键盘之间存储值

[英]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: 一些可能的解决方案是:

  1. Your app group is not setup correctly, or you are not using the correct group identifier with initWithSuiteName: 您的应用组未正确设置,或者您没有在initWithSuiteName:使用正确的组标识符initWithSuiteName:

  2. 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的共享容器

  3. It's a bug. 这是一个错误。

  1. Set

     RequestsOpenAccess = YES; 
  2. Settings: 设置:

     NSUserDefaults * usrInfo = [[NSUserDefaults alloc] initWithSuiteName:@"myKeyboard"]; [usrInfo setObject:theme.icon forKey:@"themeName"]; // This is the new data; [usrInfo synchronize]; 
  3. 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.

相关问题 发布NSUserDefaults在iOS键盘扩展和包含应用之间进行更改吗? - Post NSUserDefaults changes between iOS Keyboard Extension and Containing app? 在NSUserDefaults中存储自定义对象时,应用程序崩溃 - App crashes when storing custom object in NSUserDefaults 使用NSUserDefaults在本地存储值 - Storing values locally using NSUserDefaults 在 NSUserDefaults 中存储值有什么限制吗? - Is there any limit in storing values in NSUserDefaults? 适用于本机应用程序的iOS 8自定义键盘 - iOS 8 Custom Keyboard for native app 在自定义 iOS 键盘中检测应用程序 - In custom iOS keyboard detect the app 使用iOS 8应用程序组在两个应用程序之间共享数据(使用NSUserDefaults) - Share datas between two apps with iOS 8 App Groups (using NSUserDefaults) 用户可以在iOS应用中修改NSUserDefaults键值吗? - Can users modify NSUserDefaults key values in an iOS app? iOS自定义键盘-无法在自定义键盘的搜索字段和第三方应用程序的搜索字段之间来回移动焦点 - iOS Custom keyboard - Not able to move focus to and fro between custom keyboard's search field and the third party app’s search field 在iOS模拟器之间同步NSUserDefaults - Sync NSUserDefaults between iOS simulators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM