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. 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"].
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. I'm on Xcode 6 beta 3. Any ideas?
UPDATE Fixed in 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:
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
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:
NSUserDefaults * usrInfo = [[NSUserDefaults alloc] initWithSuiteName:@"myKeyboard"];
[usrInfo synchronize];
NSString * str = [usrInfo objectForKey:@"themeName"];
Then you can change the keyboard , for example ,change its background
I think your suite name has to start with group and match up with the container you made (source).
I found it has to follow this format: group.com.company.appname - I had something else and it didn't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With