7

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

bdev
  • 2,060
  • 5
  • 24
  • 32
  • possible duplicate of [NSUserDefault with App Group is not working in iOS 8 Beta3](http://stackoverflow.com/questions/24840993/nsuserdefault-with-app-group-is-not-working-in-ios-8-beta3) – Andrew Jul 19 '14 at 23:34
  • I have XCode 6 beta 5 and problem still persists. – iOS Dev Aug 11 '14 at 13:27

5 Answers5

7

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.

Andrew
  • 15,357
  • 6
  • 66
  • 101
  • Thank you for your answer! I've triple checked group identifier -- that is correct. To the best of my knowledge app groups have been set up correctly (just followed the steps in the Capabilities section which created entitlements for both targets with the group identifier). Great catch on enabling network access -- I changed RequestsOpenAccess to YES, wiped the simulator, and reinstalled; however, the issue still persists. I hope it's a bug! Testing on Xcode beta 4 soon...will update then. – bdev Jul 21 '14 at 17:48
4
  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

Sajjad Ali
  • 33
  • 11
SamSam
  • 113
  • 8
  • Is there a way to have read only access to settings from the keyboard and not have to ask for Open Access? – Kyle Howells Sep 23 '14 at 21:26
  • I donnot find the access up to now. If you get it, please tell me, thank you so much. – SamSam Sep 25 '14 at 02:39
  • I have similar issue like @bdev, but with Watch Kit Extension. AppGroup worked well in XCode 6.3 beta, but in XCode 6.2 it doesn't. I added `synchronize` like you recommend but it didn't help. – kelin Mar 12 '15 at 12:10
3

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

Tsvi Tannin
  • 397
  • 1
  • 2
  • 8
  • Yes, it's in the form group.mycompany.myapp. I'll edit my post to make that more clear – bdev Jul 16 '14 at 19:49
1

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

Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
0

Your group name (as set in Xcode) has to start with "group" (e.g." "group.com.myName.MyApp" (I learned this when creating a group capability since the name field is already populated with "group".) and the NSUserDefaults suiteName has to start with another "group", so "group" is there twice, e.g. "group.group.com.myName.MyApp"). I learned this by looking at the app's Library/Preferences directory when it was running in the sim. Nothing would appear there unless I used this scheme.

I still haven't been able to get the watch app to see the common group defaults file.

Jeff
  • 2,659
  • 1
  • 22
  • 41
  • As of Watch OS 2, the shared container capability has been removed by Apple. It's documented somewhere, but buried. I spent ten hours on this problem, and learned the bad news from other posts. – Jeff Sep 23 '15 at 21:24