18

I've seen all the other posts of SO about sharing NSUserDefaults data between host apps and app extensions, but mine still doesn't work. Here's everything I've done:

  1. Enable App Groups on Xcode's "Capabilities" tab for the extension and host app.
  2. Set the group name to "group.com.mycompany.foo" and confirmed the extension and host app are added to the group.
  3. Confirmed an entitlements file exists for both with the "com.apple.security.application-groups" key set to my app group name. (For what it's worth, I'm also using the shared keychain capabilities between extension and host app and that is working -- so I don't believe this is an issue with entitlements)
  4. Regenerated all provisioning profiles. In fact, I even revoked all my certificates and regenerated all profiles fresh after setting up the app group.

And after all of that, this code still doesn't work:

// in the host app
NSUserDefaults *testDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.mycompany.foo"];
[testDefaults setObject:@"TEST" forKey:@"foo"];
[testDefaults synchronize];

and then:

// in the extension
NSUserDefaults *testDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.mycompany.foo"];
NSLog(@"%@", [testDefaults objectForKey:@"foo"]);

which results in:

(null)

When I look in Xcode preferences, I notice these icons, none of which have the "App Group" icon: Xcode Preferences - Entitlements

But as I stated before, I went as far as clearing out my keychain, certs, and provisioning profiles after setting up the app group. So if this is in fact the problem, how do I add the entitlement to the profiles?

What am I missing? I'm fresh out of ideas.

Help?

EDIT: Access groups are working in my Today & Safari extension, just not in the WatchKit extension. I have no idea why, and all the build settings appear to be configured the same way.

EDIT 2: For those insisting that my capabilities aren't setup properly, here's a screenshot: Capabilities

user2393462435
  • 2,652
  • 5
  • 37
  • 45
  • Have you tried clicking the "refresh" button at the bottom left corner of Xcode's list of provisioning profiles? – Tom Harrington Mar 05 '15 at 21:14
  • 1
    Probably a million times. :( – user2393462435 Mar 05 '15 at 21:15
  • There is no App Groups icon in Entitlements column in view you attached (at least I don't have it and appgroups works for me). Back to the problem: are you 100% sure that both host app and extension target are signed with correct provisioning profile and code sign identity? And by correct I mean `com.mycompany.foo.myapp` and `com.mycompany.foo.myapp.myappwatchkitextension`. Wildcard will not work here. – lvp Mar 05 '15 at 21:54
  • FWIW, none of my profiles include the app group icon, even those that do actually work with app groups. I wouldn't pay attention to whether the icon shows in that list. If the "capabilities" section of the app target settings shows app groups enabled, that *should* be all that's needed. – Tom Harrington Mar 06 '15 at 21:16
  • Okay, I will disregard the icons. Capabilities are definitely enabled. And the provision profiles are set explicitly, without wildcards (com.myapp and com.myextension). I'm pulling my hair out, and I don't have much hair left to begin with. – user2393462435 Mar 06 '15 at 22:56
  • I also just noticed that access groups are working in my other extensions -- just not in the WatchKit extension. The plot thickens... – user2393462435 Mar 06 '15 at 23:03
  • @user2393462435 Are you building with debug configuration or release? Did you resolve this? – Rpranata Mar 12 '15 at 00:59
  • Very good question. I have the same problem. My Watch Kit App was working well in XCode 6 beta 3. But since I have used XCode 6.2 watch app could not access to the app group. I tried almost everything: reinstalling and resetting simulator, cleaning project, building for debug and release, experimenting with entitlements. Nothing helps. – kelin Mar 13 '15 at 09:55

3 Answers3

6

I had the similar problem. What I noticed, that my WatchKit Extension and WatchKit App had equal bundle identifiers. I changed bundle id of my WatchKit App. Also I didn't forgot to change NSExtensionAttributes/WKAppBundleIdentifier in the info.plist of Extension to match that id. Now everything works. I hope this will help you!

Shortly: Bundle identifiers of WatchKit App and Extension should be different!

kelin
  • 11,323
  • 6
  • 67
  • 104
2

Here is what did the trick for me. Apparently Xcode automatically created a new Entitlements file for the new Extension (probably after I set it up from "Capabilities" but unfortunately the value of the array was missing...

In the app Entitlements:

enter image description here

In the Extension Entitlements the value was missing from the array:

enter image description here

Also it's important to make sure that the group appears in the "Capability" tab for both targets (app and Extension) and that all 3 checkmarks are green, see example:

App Target

enter image description here

Extension Target

enter image description here

Oded Regev
  • 4,065
  • 2
  • 38
  • 50
1

Did you try to read the same key from the iPhone app?

I believe the problem is that you didn't activate the App Group entitlement in your WatchKit extension.

  1. Select your WatchKit Extension under your project settings.

  2. Select Capabilities tab under WatchKit Extension and look for App Groups and turn it on.

  3. Select the same App Group identifier your created before (it should appear automatically and just check it).

idmean
  • 14,540
  • 9
  • 54
  • 83
BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43
  • It's enabled and the entitlements file looks identical to the entitlements file for my extensions that already work. My build settings are properly referencing this entitlements file (I know this because the shared keychain is working despite the app group not working). – user2393462435 Mar 11 '15 at 18:47
  • Did you try to read the same key after setting it in the iPhone app? – BalestraPatrick Mar 11 '15 at 19:28
  • See the code in the original question. I'm setting and reading from the same key. – user2393462435 Mar 11 '15 at 20:16
  • This is the right answer I spent hours searching for it online. I have made bold the section where we have to select the app extension and then turn the capability on. Thanks dude! – Sam B Mar 12 '15 at 16:43
  • 2
    This is not the correct answer. Here's a screenshot of my settings. It clearly shows that this is configured correctly, but it STILL doesn't. It works in the other extensions, but not the watchkit extension: http://i.imgur.com/JR9lwaO.jpg – user2393462435 Mar 12 '15 at 17:33