ios 测试钥匙串 - OSStatus 错误 -34018

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22082996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:36:11  来源:igfitidea点击:

Testing the Keychain - OSStatus error -34018

iosiphonexcodesentestingkit

提问by Andrew

I'm trying to test code that reads and alters the keychain using the basic SenTest framework on Xcode. The code works fine on device, but when I start the test I get these errors every time I want to touch the keychain with SecItemDelete/SecItemAdd/etc.

我正在尝试使用 Xcode 上的基本 SenTest 框架来测试读取和更改钥匙串的代码。该代码在设备上运行良好,但是当我开始测试时,每次我想用SecItemDelete/ SecItemAdd/etc触摸钥匙串时都会收到这些错误。

The operation couldn't be completed. (OSStatus error -34018 - client has neither application-identifier nor keychain-access-groups entitlements)

The operation couldn't be completed. (OSStatus error -34018 - client has neither application-identifier nor keychain-access-groups entitlements)

I have matching wildcard provisioning profiles (iOS Team Provisioning Profile: *) for both the build target and the test target.

iOS Team Provisioning Profile: *对于构建目标和测试目标,我都有匹配的通配符配置文件 ( )。

These (unconfirmed) stack overflow answers:

这些(未经证实的)堆栈溢出答案:

Read from keychain results in errSecItemNotFound 25300

从钥匙串读取结果为 errSecItemNotFound 25300

say that I need a provisioning profile matching my app identifier every time I use the keychain, but that can't be right, or I'd get the same errors outside of the test target.

说我每次使用钥匙串时都需要一个与我的应用程序标识符匹配的配置文件,但这不可能是正确的,否则我会在测试目标之外遇到相同的错误。

Digging deeper, the (unconfirmed) answers here:

深入挖掘,(未经证实的)答案在这里:

SecItemAdd and SecItemCopyMatching returns error code -34018 (errSecMissingEntitlement)

SecItemAdd 和 SecItemCopyMatching 返回错误代码 -34018 (errSecMissingEntitlement)

imply that there might be a bug within keychain and more generally Security.framework, which is frankly terrifying.

暗示钥匙串中可能存在错误,更普遍的是Security.framework,这坦率地说是可怕的。

My question is; has anyone hit OSStatus error -34018 onlywhen they were on a test-target? That appears to be the behavior I am seeing.

我的问题是;有没有人只有在测试目标上时才遇到 OSStatus 错误 -34018 ?这似乎是我所看到的行为。

EDIT: Adding this answerthat JorgeDeCorteused in his answer below.

编辑:添加JorgeDeCorte在下面的答案中使用的这个答案。

This thread seems to contain the solution if the problem exits in your unit-test target.

https://devforums.apple.com/message/917498#917498

Basically you have to codesign your .xcttest folder by adding the following as a run script in your test target.

codesign --verify --force --sign "$CODE_SIGN_IDENTITY" "$CODESIGNING_FOLDER_PATH"

I got a lot of -34018 errors when testing my keychain on the device and this managed to fix it.

If the problem does not exist in your test target this is probably not the solution.

如果您的单元测试目标中存在问题,则此线程似乎包含解决方案。

https://devforums.apple.com/message/917498#917498

基本上,您必须通过在测试目标中添加以下作为运行脚本来对 .xcttest 文件夹进行协同设计。

codesign --verify --force --sign "$CODE_SIGN_IDENTITY" "$CODESIGNING_FOLDER_PATH"

在设备上测试我的钥匙串时,我遇到了很多 -34018 错误,这设法修复了它。

如果您的测试目标中不存在该问题,则这可能不是解决方案。

So I guess the solution is: force sign your test target.

所以我想解决方案是:强制签署您的测试目标。

采纳答案by JorgeDeCorte

To answer your question: Yes, I experience the same problem. It seems to work fine when running my app. But when I run my XCTests on my device it seems that the keychain returns error -34018. The strange thing is that it doesn't happen when I run the tests on the simulator.

回答您的问题:是的,我遇到了同样的问题。运行我的应用程序时它似乎工作正常。但是当我在我的设备上运行 XCTests 时,钥匙串似乎返回错误 -34018。奇怪的是,当我在模拟器上运行测试时,它不会发生。

EDIT: I found a solution which I have explained in this answer

编辑:我找到了我在这个答案中解释的解决方案

回答by Alex Stone

I got this error when attempting to run keychain operations via Grand Central Dispatch. Find a way to instantiate your keychain (or keychain wrapper) on the main thread.

尝试通过 Grand Central Dispatch 运行钥匙串操作时出现此错误。找到一种方法在主线程上实例化您的钥匙串(或钥匙串包装器)。

//results in code -34018
   static dispatch_once_t token;
    dispatch_once(&token, ^{
        keychain = [[KeychainWrapper alloc] init];

    });

//works fine
keychain = [[KeychainWrapper alloc] init];

回答by vomako

I also got the "OSStatus error -34018". I solved it by recreating my provisioning profile.

我还收到了“OSStatus 错误 -34018”。我通过重新创建我的配置文件来解决它。

回答by Patrik

Codesigning a .xctest bundle isn't as easy as it sounds in some cases. Principally JorgeDeCorte is right with his answerthat the given short line as a Run Scriptis enough for most of the devs.

在某些情况下,编码 .xctest 包并不像听起来那么容易。主要 JorgeDeCorte 的回答是正确的,即给定的短线 aRun Script对大多数开发人员来说已经足够了。

codesign --verify --force --sign "$CODE_SIGN_IDENTITY" "$CODESIGNING_FOLDER_PATH"

But when you have multiple certificates in your keychain this will fail with the following line

但是当您的钥匙串中有多个证书时,这将失败并显示以下行

iPhone Developer: ambiguous (matches "iPhone Developer: Your Name (ABC123DEF45)" and "iPhone Developer: Your Name (123ABC456DE)"

A solution to get the right certificate even with multiple ones is this short script. For sure this is not ideal, but as of my knowledge you have no chance to get the certificate that Xcode found and uses for signing your .app.

即使有多个证书也能获得正确证书的解决方案是这个简短的脚本。当然这并不理想,但据我所知,您没有机会获得 Xcode 找到并用于签署您的 .app 的证书。

echo "codesign --verify --force --sign \"$CODE_SIGN_IDENTITY\" \"$CODESIGNING_FOLDER_PATH\""
IDENTITIES=`security find-identity -v -s "Code Signing" | grep "iPhone Developer" | awk '{ print  }'`

for SHA in $IDENTITIES; do
    codesign --verify --force --sign $SHA "$CODESIGNING_FOLDER_PATH"
    if [ $? -eq 0 ]; then
        echo "Matching identity found: $SHA"
        exit 0
    fi
done;

exit 1

回答by kgaidis

I was also unable to run tests that involved the Keychain.

我也无法运行涉及钥匙串的测试。

What worked for me was adding a "Host Application" for the tests.You can find this by going to your project targets, tapping "MyTestTarget", click the "General" tab and select a "Host Application" with a drop-down.

对我有用的是为测试添加一个“主机应用程序”。您可以通过转到您的项目目标,点击“MyTestTarget”,单击“常规”选项卡并选择带有下拉列表的“主机应用程序”来找到它。

I was testing a CocoaTouch Framework so there was no host application for it. I had to create one (ex. "MyFrameworkTestApp") just for the tests.

我正在测试 CocoaTouch 框架,因此没有适用于它的主机应用程序。我不得不为测试创建一个(例如“MyFrameworkTestApp”)。

If that didn't help, you can also try to go to the "Capabilities" tab for the "MyFrameworkTestApp" target and try enabling the "Keychain Sharing" capability.

如果这没有帮助,您还可以尝试转到“MyFrameworkTestApp”目标的“功能”选项卡并尝试启用“钥匙串共享”功能。