NSUserDefaults standardUserDefaults does not work with extension

I have added application groups to my application identifier on the developer portal and use this application identifier in my provisioning profile. My product ID in Xcode is set to this application ID.

In my application, 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 is not true. If I delete the verification check and just print the result, the user keyboard will fail.

EDIT Also tried this with the same result:

App delegation

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 Features section of both goals, I have groups included with the selected group identifier.

What am I missing here?

+4
3
  • RequestsOpenAccess = YES;
    
  • :

    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"];
    

, ,

+1

iOS " ". .

- , , "" , .

+1

. " ", - . " " NSUserDefaults.standardUserDefaults

The problem is that when you change NSUserDefaults from an application group, it works until you restart the application because it also stores values ​​in memory. It’s hard to debug.

I keep the value for both: application groups NSUserDefaultsand NSUserDefaults.standardUserDefaults. And when reading - a check of both too.

0
source

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


All Articles