Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 985
bdev Avatar asked Jul 16 '14 18:07

bdev


4 Answers

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.

like image 127
Andrew Avatar answered Nov 09 '22 04:11

Andrew


  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 28
SamSam Avatar answered Nov 09 '22 06:11

SamSam


I think your suite name has to start with group and match up with the container you made (source).

like image 25
Tsvi Tannin Avatar answered Nov 09 '22 05:11

Tsvi Tannin


I found it has to follow this format: group.com.company.appname - I had something else and it didn't work.

like image 25
Johnny Rockex Avatar answered Nov 09 '22 04:11

Johnny Rockex