Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults standardUserDefaults not working with extension

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?

like image 512
Spentak Avatar asked Oct 31 '22 20:10

Spentak


1 Answers

  1. Set

    RequestsOpenAccess = YES;
    
  2. Settings:

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

like image 154
SamSam Avatar answered Nov 15 '22 05:11

SamSam