NSUserDefaults does not save values ​​between application and user keyboard in iOS 8

I am trying to exchange data between my application and a custom keyboard extension. I have included application groups in both the main target program and the target user keyboard. In my main application, I add an object with the following:

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myapp"]; [userDefaults setObject:someObject forKey:@"KEY"]; 

A listing of the [userDefaults dictionaryRepresentation] in the console indicates that this object has been saved, and also calls [userDefaults objectForKey: @ "KEY"].

However, when I try to access this object in a custom keyboard extension:

 NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myapp"]; NSString *value = [userDefaults objectForKey:@"KEY"]; 

The value is nil, and the call to [userDefaults dictionaryRepresentation] does not show the record that was saved above. I'm on Xcode 6 beta 3. Any ideas?

UPDATE Fixed in Xcode 6 beta 5

+6
source share
5 answers

Several possible solutions:

  • Your application group is not configured correctly or you are not using the correct group identifier with initWithSuiteName:

  • You have not enabled network access for your keyboard. This document states the following when network access is disabled for your keyboard (default behavior):

    Missing shared container with content

  • This is mistake.

+6
source
  • Set

     RequestsOpenAccess = YES; 
  • Settings:

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

+4
source

I think your package name should start with a group and match your container ( source ).

+2
source

The name of your group (as indicated in Xcode) should start with "group" (for example, " group.com.myName.MyApp " (I learned about this when creating a group feature, because the name of the field is already filled with "group".) And the name NSUserDefaults device_name should start with another "group", so the "group" is present twice, for example " group.group.com.myName.MyApp ") I found out about this by looking at the application directory / Settings library when it worked in the sim. Nothing would have appeared there if I had not used this scheme.

I still have not been able to get the application to view shared file files by default.

0
source

I found that he should follow this format: group.com.company.appname - I had something else and it didn’t work.

0
source

Source: https://habr.com/ru/post/972339/


All Articles