问题
I added App Groups to my app ID in the developer portal and am using that App ID in my provisioning profile. My Product Identifier in Xcode is set to that app ID.
In my app delegate I call this from didFinishLaunchingWithOptions
NSUserDefaults.standardUserDefaults().setObject("hey", forKey: "TEST")
NSUserDefaults.standardUserDefaults().synchronize()
In my keyboard app extension I call this:
if let test = NSUserDefaults.standardUserDefaults().objectForKey("TEST") as? String
{
println(test)
}
This never is true. If I remove the validation test and just print the result the custom keyboard crashes.
EDIT Have also tried this with same crash result:
App Delegate
var userDefaults = NSUserDefaults(suiteName: "group.jackedgames.myappgroup")
userDefaults.setObject("TEST", forKey: "TEST")
userDefaults.synchronize()
Keyboard Extension
var userDefaults = NSUserDefaults(suiteName: "group.jackedgames.myappgroup")
var test = userDefaults.objectForKey("TEST") as String
NSLog(test)
In the "Capabilities" section of both targets I have the groups enabled with this group ID selected.
What am I missing here?
回答1:
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
回答2:
Remove your keyboard from the iOS settings menu and add it again with "allow full access permission". It should work.
If it still doesn't work, check for both containing application and keyboard extension in target configuration in "Capabilities" section in app group that there are everything is fixed.
回答3:
Just stepped on this. In keyboard extension you need "Full Access" to have access to write something to the app group files. So without "Full Access" you need to use NSUserDefaults.standardUserDefaults
The problem is that when you're changing NSUserDefaults from app group - it is working until you restart the app, because it stores values in memory too. Hard to debug.
I'm saving value to both: app group NSUserDefaults
and NSUserDefaults.standardUserDefaults
. And when reading - checking both too.
来源:https://stackoverflow.com/questions/24620622/nsuserdefaults-standarduserdefaults-not-working-with-extension