5

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?

Spentak
  • 3,359
  • 3
  • 21
  • 31
  • did you call `NSUserDefaults.standardUserDefaults().synchronize()`? – Jack Jul 07 '14 at 22:04
  • Yes. But i'm pretty sure that synchronize() is an optional call anyway – Spentak Jul 07 '14 at 22:05
  • 1
    See http://stackoverflow.com/questions/24064722/save-and-load-data-on-today-extensions-ios-8 – Jack Jul 07 '14 at 22:07
  • `synchronize()` is *usually* optional... – nhgrif Jul 07 '14 at 22:08
  • This really probably shouldn't be marked as a duplicate of an Objective-C question. – nhgrif Jul 07 '14 at 22:11
  • @nhgrif This is a Cocoa Touch, iOS 8 question independent of language. The same exact thing can be written in ObjC – Jack Jul 07 '14 at 22:14
  • It sort of can, yes... but as someone who knows Objective-C and hasn't spent a ton of time with Swift yet, I can't see what's wrong with this, and as such I assume it's something specific to Swift syntax. And if it is a problem with the Swift syntax, then again... it really shouldn't be marked as a duplicate. – nhgrif Jul 07 '14 at 22:16
  • @nhgrif I agree and understand what you are saying. It doesn't apply here though. The solution is to use new API introduced in iOS 8 (`initWithSuiteName`) with App Groups and has nothing to do with syntax. – Jack Jul 07 '14 at 22:18
  • Oh, I see now. I didn't look at the answers to that question. – nhgrif Jul 07 '14 at 22:25
  • Unfortunately the solutions on that question do not work. I am wondering if NSUserDefaults is not working on Keyboard extensions yet – Spentak Jul 07 '14 at 22:35

3 Answers3

1
  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

SamSam
  • 113
  • 8
  • Thanks, man. I have seen so many people sharing the code and missing the important step to add RequestsOpenAccess in info.plist. – Faisal Khalid Apr 20 '19 at 09:11
1

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.

Tatiana
  • 390
  • 8
  • 23
0

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.

Gleb Tarasov
  • 885
  • 11
  • 15