Search code examples
iosobjective-cnsuserdefaultsios8ios-app-extension

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. 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


Solution

  • A few probable solutions are:

    1. Your app group is not setup correctly, or you are not using the correct group identifier with 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

    3. It's a bug.