简体   繁体   English

为什么我的WatchKit扩展程序中的应用程序组不起作用?

[英]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. 我已经看过关于在主机应用和应用扩展之间共享NSUserDefaults数据的所有其他帖子,但我仍然无法工作。 Here's everything I've done: 以下是我所做的一切:

  1. Enable App Groups on Xcode's "Capabilities" tab for the extension and host app. 在Xcode的“功能”选项卡上为扩展和主机应用启用应用组。
  2. Set the group name to "group.com.mycompany.foo" and confirmed the extension and host app are added to the group. 将组名设置为“group.com.mycompany.foo”,并确认扩展名和主机应用程序已添加到组中。
  3. Confirmed an entitlements file exists for both with the "com.apple.security.application-groups" key set to my app group name. 已确认两者都存在权利文件,并且“com.apple.security.application-groups”键设置为我的应用程序组名称。 (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首选项时,我注意到这些图标,其中没有一个具有“App Group”图标: 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. 编辑 :访问组正在我的今日和Safari扩展中工作,而不是在WatchKit扩展中。 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: 编辑2 :对于那些坚持我的功能设置不正确的人,这里有一个截图: 功能

I had the similar problem. 我有类似的问题。 What I noticed, that my WatchKit Extension and WatchKit App had equal bundle identifiers. 我注意到,我的WatchKit扩展和WatchKit应用程序具有相同的包标识符。 I changed bundle id of my WatchKit App. 我更改了WatchKit App的bundle id。 Also I didn't forgot to change NSExtensionAttributes/WKAppBundleIdentifier in the info.plist of Extension to match that id. 此外,我没有忘记在Extension的info.plist中更改NSExtensionAttributes / WKAppBundleIdentifier以匹配该ID。 Now everything works. 现在一切正常。 I hope this will help you! 我希望这能帮到您!

Shortly: Bundle identifiers of WatchKit App and Extension should be different! 简而言之WatchKit App和Extension的捆绑标识符应该是不同的!

Did you try to read the same key from the iPhone app? 您是否尝试从iPhone应用程序中读取相同的密钥?

I believe the problem is that you didn't activate the App Group entitlement in your WatchKit extension. 我认为问题在于您未在WatchKit扩展程序中激活App Group授权。

  1. Select your WatchKit Extension under your project settings. 在项目设置下选择WatchKit Extension。

  2. Select Capabilities tab under WatchKit Extension and look for App Groups and turn it on . 选择WatchKit Extension下的Capabilities选项卡,然后查找App Groups并将其打开

  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... 显然Xcode会自动为新扩展创建一个新的Entitlements文件(可能在我从“Capabilities”设置之后,但不幸的是数组的值丢失了......

In the app Entitlements: 在应用程序权利中:

在此输入图像描述

In the Extension Entitlements the value was missing from the array: 在Extension Entitlements中,数组中缺少值:

在此输入图像描述

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: 同样重要的是要确保该组出现在两个目标(应用程序和扩展程序)的“功能”选项卡中,并且所有3个复选标记都是绿色,请参阅示例:

App Target 应用目标

在此输入图像描述

Extension Target 扩展目标

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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