[英]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:
以下是我所做的一切:
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”图标:
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授权。
Select your WatchKit Extension under your project settings. 在项目设置下选择WatchKit Extension。
Select Capabilities tab under WatchKit Extension and look for App Groups and turn it on . 选择WatchKit Extension下的Capabilities选项卡,然后查找App Groups并将其打开 。
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.