简体   繁体   中英

Why won't app groups work inside my WatchKit extension?

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首选项 - 权利

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: 功能

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!

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).

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:

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

Extension Target

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM