xcode containerURLForSecurityApplicationGroupIdentifier 在 iOS 模拟器上返回 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26740404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
containerURLForSecurityApplicationGroupIdentifier returns nil on iOS Simulator
提问by Francescu
Context
语境
With iOS8 and extensions Apple introduced the App group containers (more info here).
通过 iOS8 和扩展,Apple 引入了 App 组容器(更多信息请点击此处)。
Problem
问题
We use it through NSFileManager
's containerURLForSecurityApplicationGroupIdentifier:
method. It works well in production on AppStore (with both iOS8 and iOS7). Problem is since our team updated to Xcode 6.1 (6A1052d)
, the method returns nil
on Simulators.
我们使用 throughNSFileManager
的containerURLForSecurityApplicationGroupIdentifier:
方法。它在 AppStore 上运行良好(iOS8 和 iOS7)。问题是自从我们的团队更新到 后Xcode 6.1 (6A1052d)
,该方法会nil
在模拟器上返回。
We searched a lot but we didn't manage to find any clues. Even this questionor this oneare not applicable here.
我们搜索了很多,但没有找到任何线索。即使这个问题或这一个是在这里不适用。
TLDR: Question
TLDR:问题
Does NSFileManager
's containerURLForSecurityApplicationGroupIdentifier:
method works on your simulators? Is there any way to fix that?
NSFileManager
的containerURLForSecurityApplicationGroupIdentifier:
方法是否适用于您的模拟器?有什么办法可以解决这个问题吗?
采纳答案by Matthieu Rouif
I was able to solve this problem on my side. The documentation says
我能够在我这边解决这个问题。文档说
Your app must have a com.apple.security.application-groups entitlement for the specified application group. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/occ/instm/NSFileManager/containerURLForSecurityApplicationGroupIdentifier:
您的应用程序必须具有指定应用程序组的 com.apple.security.application-groups 权利。https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/occ/instm/NSFileManager/containerURLForSecurityApplicationGroupIdentifier:
At some point in my app, I had removed the entitlements file from my app target/build settings/code signing/code signing entitlements . Both debug and release must include YOUR_APP.entitlements file . My debug wasn't including it so it wasn't working on simulator
在我的应用程序的某个时刻,我从我的应用程序目标/构建设置/代码签名/代码签名权利中删除了权利文件。调试和发布都必须包含 YOUR_APP.entitlements 文件。我的调试不包括它所以它不能在模拟器上工作
回答by Juan Fran Jimenez
The method containerURLForSecurityApplicationGroupIdentifier:
works in both device and Simulator.
该方法containerURLForSecurityApplicationGroupIdentifier:
适用于设备和模拟器。
If you have setup both targets (or Target and WatchKit Extension for instance) that are going to share data to support Groups, and it keeps returning nil, you may want to check your Targets settings.
如果您已设置要共享数据以支持组的两个目标(或例如 Target 和 WatchKit 扩展),并且它一直返回nil,您可能需要检查您的目标设置。
Target Settings > General > Team > yourCompanysigning team should be selected here. If there is a warning and a Fix This Issuebutton, be good and fix the issue by hitting it :)
Target Settings > Capabilities > App Groups > Is On + shows your group.com.yourcompany.yourapp.sharedContaintergroup name ("sharedContainer" is my choice name) is selected and is on black (not red). If it's red or there is a warning and Fix This Issue button, renew the group or create a different one.
Target Settings > Build Settings > Code Signing > Code Signing Entitlements: points to your entitlements files for both Debug and Release (something like yourApp/yourApp.entitlements)
Target Settings > Build Settings > Code Signing > Code Signing Identity: Make sure the identity is part of the Team selected above. I use iOS Developer.
Target Settings > Build Settings > Code Signing > Provisioning Profile: As above, make sure the PP you are using is of the Team required. I set it to Automatic.
应在此处选择目标设置 > 常规 > 团队 > yourCompany签约团队。如果有警告和“修复此问题”按钮,请通过点击它来解决问题:)
目标设置 > 功能 > 应用程序组 > 开启 + 显示您的 group.com.yourcompany.yourapp.sharedContainter组名称(“sharedContainer”是我选择的名称)被选中并且显示为黑色(不是红色)。如果它是红色的或有警告和修复此问题按钮,请更新组或创建不同的组。
目标设置 > 构建设置 > 代码签名 > 代码签名权利:指向调试和发布的权利文件(类似于yourApp/yourApp.entitlements)
目标设置 > 构建设置 > 代码签名 > 代码签名身份:确保身份是上面选择的团队的一部分。我使用iOS 开发人员。
目标设置 > 构建设置 > 代码签名 > 供应配置文件:如上所述,确保您使用的 PP 是所需的团队。我将其设置为Automatic。
Check those for both targetssharing data.
检查两个目标共享数据的那些。
I hope it helps
我希望它有帮助
回答by respectTheCode
It doesn't make it clear the the docs but I found that the group identifier is case sensitive.
它没有明确文档,但我发现组标识符区分大小写。
回答by superZhen
I find containerURLForSecurityApplicationGroupIdentifier can do work both on real device and simulator with xcode 6.2. I came across into this thread because i also got nil, but now i find the root cause is my typing error of the 'App Groups' name.
我发现 containerURLForSecurityApplicationGroupIdentifier 可以在真实设备和模拟器上使用 xcode 6.2 工作。我遇到了这个线程,因为我也没有,但现在我发现根本原因是我的“应用程序组”名称输入错误。
More, here is the list you need check:
更多,这是您需要检查的列表:
- make sure 'app groups' been created either by xcode or web console at https://developer.apple.com
- make sure the 'app groups' is 'enabled' on 'Development' and 'Production' at 'web console: Identifiers->App IDs'
- make sure the matched 'Provisioning Profiles' are 'active'
- the groups name of entitlement in both containing app and extension app should match the one you created in step 1
- make sure the bundle identifier of all targets is the same as the one from 'web console: Identifiers->App IDs
- make sure the 'Team' inside of all targets matches the one from web console
- make sure to pick the correct group under 'Capabilities' tab on both containing app and extension app target, and no red color
- clean up xcode cache by cmd+shift+k
- rebuild and pray
- 确保通过https://developer.apple.com 上的xcode 或 Web 控制台创建“应用程序组”
- 确保在“Web 控制台:标识符->应用程序 ID”的“开发”和“生产”上“启用”“应用程序组”
- 确保匹配的“配置文件”是“活动的”
- 包含应用程序和扩展应用程序中的权利组名称应与您在步骤 1 中创建的名称相匹配
- 确保所有目标的捆绑标识符与来自“Web 控制台:标识符->应用程序 ID”的标识符相同
- 确保所有目标中的“团队”与 Web 控制台中的“团队”匹配
- 确保在包含应用程序和扩展应用程序目标的“功能”选项卡下选择正确的组,并且没有红色
- 通过 cmd+shift+k 清理 xcode 缓存
- 重建和祈祷
回答by George
I've found that the reason of this error in my case was a file ".com.apple.mobile_container_manager.metadata.plist". After I've got my file from this directory, I deleted all files in it. So this file was deleted also. And when you delete this file, app returns you a nil
for containerURLForSecurityApplicationGroupIdentifier
我发现在我的情况下这个错误的原因是一个文件".com.apple.mobile_container_manager.metadata.plist"。从该目录中获取文件后,我删除了其中的所有文件。所以这个文件也被删除了。当您删除此文件时,应用程序会返回一个nil
forcontainerURLForSecurityApplicationGroupIdentifier
So I changed my code to this:
所以我把我的代码改成这样:
NSURL *groupPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:YGGroupIdentifier];
NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:[groupPath path] error:nil];
for (NSString *filename in fileArray) {
if (![filename hasPrefix:@".com"]) {
[fileManager removeItemAtPath:[[groupPath path] stringByAppendingPathComponent:filename] error:nil];
}
}
Now it works great, even in simulator.
现在它工作得很好,即使在模拟器中也是如此。
回答by martn_st
For me there was some mess with my iOS Team Provisioning Profile(which is not required to run in Simulator).
对我来说,我的iOS 团队配置文件(不需要在模拟器中运行)有些混乱。
In order to run my app on my iPhone I had to first reconfigure the iOS Team Provisioning Profilefor the Debug environment. Calling containerURLForSecurityApplicationGroupIdentifier:
worked perfectly fine on the device. Afterwards I went back to the Simulator and - there you go - it worked again as well.
为了在我的 iPhone 上运行我的应用程序,我必须首先为调试环境重新配置iOS 团队配置文件。通话containerURLForSecurityApplicationGroupIdentifier:
在设备上运行良好。之后我又回到了模拟器——你去了——它也再次运行了。
回答by lahmar
The Method containerURLForSecurityApplicationGroupIdentifier:
works in my Simulator. But if i delete all files in the group folder (in case a user performs a logout in my app) the method returns nil on the next RUN in Xcode 6.1. I tested this with Xcode 6.2 and iOS SDK 8.2 Beta as well but it doesn't work.
该方法containerURLForSecurityApplicationGroupIdentifier:
适用于我的模拟器。但是,如果我删除组文件夹中的所有文件(以防用户在我的应用程序中执行注销),则该方法在 Xcode 6.1 中的下一次运行时返回 nil。我也用 Xcode 6.2 和 iOS SDK 8.2 Beta 对此进行了测试,但它不起作用。
The Code runs fine on a real device.
该代码在真实设备上运行良好。
I also tried the above solutions without success.
我也尝试了上述解决方案,但没有成功。