SecItemAdd 在 iOS 10 模拟器的 Xcode 8 中总是返回错误 -34018
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38456471/
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
SecItemAdd always returns error -34018 in Xcode 8 in iOS 10 simulator
提问by Evgenii
Update: This issue has been fixed in Xcode 8.2. Keychain works in the simulator without enabling keychain sharing.
更新:此问题已在 Xcode 8.2 中修复。钥匙串在模拟器中工作,无需启用钥匙串共享。
Why am I alwaysreceiving error -34018 when calling SecItemAdd
function in Xcode 8 / iOS 10 simulator?
为什么在 Xcode 8 / iOS 10 模拟器中调用函数时总是收到错误 -34018 ?SecItemAdd
Steps to Reproduce
重现步骤
Create a new Single page iOS app project in Xcode 8.
Run the following code in viewDidLoad
(or open thisXcode project).
在 Xcode 8 中创建一个新的单页 iOS 应用项目。在viewDidLoad
(或打开此Xcode 项目)中运行以下代码。
let itemKey = "My key"
let itemValue = "My secretive bee "
// Remove from Keychain
// ----------------
let queryDelete: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject
]
let resultCodeDelete = SecItemDelete(queryDelete as CFDictionary)
if resultCodeDelete != noErr {
print("Error deleting from Keychain: \(resultCodeDelete)")
}
// Add to keychain
// ----------------
guard let valueData = itemValue.data(using: String.Encoding.utf8) else {
print(" Error saving text to Keychain")
return
}
let queryAdd: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject,
kSecValueData as String: valueData as AnyObject,
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked
]
let resultCode = SecItemAdd(queryAdd as CFDictionary, nil)
if resultCode != noErr {
print(" Error saving to Keychain: \(resultCode).")
} else {
print(" Saved to keychain successfully.")
}
Expected Results
预期成绩
Item is added to Keychain.
项目被添加到钥匙串。
Actual Results
实际结果
Function SecItemAdd returns the following error code: -34018
.
函数 SecItemAdd 返回以下错误代码:-34018
。
Version
版本
Xcode version 8.1 (8B62), macOS Sierra 10.12.1.
Xcode 版本 8.1 (8B62),macOS Sierra 10.12.1。
Configuration
配置
Always occurs in Xcode 8 since Beta 2 when testing in an iOS 10 simulator.
从 Beta 2 开始,在 iOS 10 模拟器中测试时总是出现在 Xcode 8 中。
Does NOT occur in Xcode 8 when testing in an iOS 9.3 simulator.
在 iOS 9.3 模拟器中测试时不会发生在 Xcode 8 中。
Demo
演示
https://dl.dropboxusercontent.com/u/11143285/2016/07/KeychainBugDemo.zip
https://dl.dropboxusercontent.com/u/11143285/2016/07/KeychainBugDemo.zip
References
参考
Radar: https://openradar.appspot.com/27422249
雷达:https: //openradar.appspot.com/27422249
Apple Developer Forums: https://forums.developer.apple.com/message/179846
Apple 开发者论坛:https: //forums.developer.apple.com/message/179846
This issue is different from the following post because it occurs consistentlyin Xcode 8. SecItemAdd and SecItemCopyMatching returns error code -34018 (errSecMissingEntitlement)
此问题与以下帖子不同,因为它在 Xcode 8 中始终存在。SecItemAdd 和 SecItemCopyMatching 返回错误代码 -34018 (errSecMissingEntitlement)
回答by Deyton
I was able to work around this in my app by adding Keychain Access Groupsto the Entitlements file. I turned on the Keychain Sharingswitch in the Capabilitiessection in your test app, and it is working for me as well.
通过将钥匙串访问组添加到权利文件,我能够在我的应用程序中解决这个问题。我在您的测试应用程序的“功能”部分中打开了钥匙串共享开关,它也对我有用。
Item to add to entitlements:
要添加到权利的项目:
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.evgenii.KeychainBugDemo</string>
</array>
I have only tried this on macOS Sierra (10.12), so I'm not sure if it will work for you on 10.11.5.
我只在 macOS Sierra (10.12) 上试过这个,所以我不确定它在 10.11.5 上是否适合你。
回答by Tiago Almeida
In Xcode 8.1 GM Release NotesApple acknowledged the problem and suggested a cleaner workaround:
在Xcode 8.1 GM Release Notes 中,Apple 承认了这个问题并提出了一个更简洁的解决方法:
Keychain APIs may fail to work in the Simulator if your entitlements file doesn't contain a value for the application-identifier entitlement. (28338972) Workaround: Add a user-defined build setting to your target named ENTITLEMENTS_REQUIRED and set the value to YES. This will cause Xcode to automatically insert an application-identifier entitlement when building.
如果您的权利文件不包含应用程序标识符权利的值,则钥匙串 API 可能无法在模拟器中工作。(28338972) 解决方法:将用户定义的构建设置添加到名为 ENTITLEMENTS_REQUIRED 的目标,并将值设置为 YES。这将导致 Xcode 在构建时自动插入应用程序标识符权利。
Note that from what I have tried, it only works in Xcode 8.1. Although the text can mislead you into a build setting, what you need to do is add this to your Environment Variables, in your scheme.
请注意,根据我的尝试,它仅适用于 Xcode 8.1。尽管文本可能会误导您进入构建设置,但您需要做的是将其添加到您的方案中的环境变量中。
Xcode 8.2 will solve this:
Xcode 8.2 将解决这个问题:
Resolved in Xcode 8.2 beta - IDE Keychain APIs work correctly in Simulator. (28338972)
在 Xcode 8.2 beta 中解决 - IDE Keychain APIs 在模拟器中正常工作。(28338972)
回答by Mustafa
回答by kavita patel
I got an error while signing with email, creating a new user or with sign out using firebase.
我在使用电子邮件签名、创建新用户或使用 firebase 注销时遇到错误。
The error was:
错误是:
firauth error domain code 17995
firauth 错误域代码 17995
I turned on the Keychain Sharing switch in the Capabilities section in your test app, and it is working for me as well.
我在您的测试应用程序的“功能”部分中打开了钥匙串共享开关,它也对我有用。
回答by ahtierney
I was looking for a solution that didn't use Keychain sharing, as that wasn't the feature I was looking for. The developer forumSeems to have a good work around from EvergreenCoder that you can limit in scope to only the iOS 10 simulator (as this seems to be the only affected simulator). From the post:
我正在寻找一种不使用钥匙串共享的解决方案,因为这不是我正在寻找的功能。开发者论坛似乎对 EvergreenCoder 有一个很好的解决方法,您可以将范围限制为仅 iOS 10 模拟器(因为这似乎是唯一受影响的模拟器)。从帖子:
The issue seems to be that there must be at least one entitlement in order for Xcode to properly add the "application-identifier" enttilement to the built application. This is why keychain sharing seems to be a solution but it is only indirectly so: any other entitlement seems to work fine.
问题似乎是必须至少有一项权利才能让 Xcode 正确地将“应用程序标识符”权利添加到构建的应用程序中。这就是为什么钥匙串共享似乎是一种解决方案,但它只是间接的:任何其他权利似乎都可以正常工作。
You can create a .plist
like so:
你可以.plist
像这样创建一个:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-/
<plist version="1.0">
<dict>
<key>get-task-allow</key>
<true/>
</dict>
</plist>
and provide a path to that file under Build Settings in
并在 Build Settings 下提供该文件的路径
Code Signing->Debug->Simulater iOS 10 SDK->($SRCROOT)/your-path-to-file
Code Signing->Debug->Simulater iOS 10 SDK->($SRCROOT)/your-path-to-file
As stated in the post, this entitlement just allows the debugger to be attached.
如帖子中所述,此权利仅允许附加调试器。
回答by johnrechd
I had a similar issue, though I was getting the -34018 error when trying to run on device. I am using XCode 8.1 on Sierra with iOS 10.1. I work on a team and suddenly had this issue when we switched to "Automatically manage signing" in the project settings. When I turn this off and manually select my profile, everything works fine. I ended up having to delete my developer certificate from my keychain, then re-select "Automatically manage signing". On the next build, it generated a new signing certificate for me and everything works fine now. I'm still not sure what caused the issue as the other cert worked fine when manually selected, but not when being managed by XCode. Hope this helps stop an hours long headache for someone else.
我遇到了类似的问题,但在尝试在设备上运行时出现 -34018 错误。我在 iOS 10.1 的 Sierra 上使用 XCode 8.1。我在一个团队工作,当我们在项目设置中切换到“自动管理签名”时,突然遇到了这个问题。当我关闭它并手动选择我的个人资料时,一切正常。我最终不得不从我的钥匙串中删除我的开发人员证书,然后重新选择“自动管理签名”。在下一个版本中,它为我生成了一个新的签名证书,现在一切正常。我仍然不确定是什么导致了这个问题,因为另一个证书在手动选择时可以正常工作,但在由 XCode 管理时就不行了。希望这有助于停止对其他人长达数小时的头痛。
回答by Adam Johns
I was able to solve this issue in Xcode 11 without any entitlement adjustments.
我能够在 Xcode 11 中解决这个问题,而无需任何权利调整。
I simply added a new app target to my framework's project called MyFrameworkTestsHostApp.
我只是在我的框架项目中添加了一个名为 MyFrameworkTestsHostApp 的新应用程序目标。
Then I selected the MyFrameworkTests target and chose its Host Application as MyFrameworkTestsHostApp.
然后我选择了 MyFrameworkTests 目标并选择它的主机应用程序作为 MyFrameworkTestsHostApp。
回答by Uchenna Nnodim
There 3 steps to take to resolve this issue quickly.
有 3 个步骤可以快速解决此问题。
- Turn on Keychain sharing in your project capabilities.
- Select Automatic Provisioning with a profile
- Ensure your custom entitlement option is set to Entitlement.plist.
- 在您的项目功能中打开钥匙串共享。
- 使用配置文件选择自动配置
- 确保您的自定义权利选项设置为 Entitlement.plist。
This will do the magic
这会变魔术
回答by Vid
It works after enabling the keychain sharing in capabilities.
它在功能中启用钥匙串共享后工作。