Xcode 8
iOS 10
SecItemAdd error
-34018 error
iOS simulator issues

SecItemAdd always returns error -34018 in Xcode 8 in iOS 10 simulator

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

SecItemAdd returning -34018 in Xcode 8 on the iOS 10 simulator was a well-known frustration because it often looked like a keychain-entitlement bug even when the app setup was fine. In that environment, the simulator itself was frequently the source of the problem, so the correct fix depended on separating real entitlement issues from simulator-only behavior.

What Error -34018 Means

The OSStatus value -34018 is commonly associated with missing or unavailable entitlements for keychain access. In a normal device environment, that points you toward signing, provisioning, or keychain-sharing configuration.

A basic keychain write looks like this:

swift
1import Security
2
3let query: [String: Any] = [
4    kSecClass as String: kSecClassGenericPassword,
5    kSecAttrAccount as String: "demo-user",
6    kSecValueData as String: Data("secret".utf8)
7]
8
9let status = SecItemAdd(query as CFDictionary, nil)
10print(status)

If status prints -34018, the system rejected the keychain operation before storing the item.

Why It Happened So Often in Xcode 8 and iOS 10 Simulator

Historically, many developers saw this error only in the simulator and not on a real device. That strongly suggests a simulator/runtime bug or mismatch in how entitlements were handled for keychain operations in that toolchain combination.

So the key diagnostic question is:

  • does the same code fail on a physical iPhone, or only in the simulator?

If it works on a device but fails in the iOS 10 simulator, your app logic is probably not the real problem.

First Checks to Make

Even though the simulator was often at fault, you should still verify the app setup:

  1. confirm the target signs correctly
  2. confirm the bundle identifier is stable
  3. enable Keychain Sharing in Signing and Capabilities if the app expects it
  4. clean and rebuild the project

If your code uses an access group, make sure that value matches the app's signed entitlements. A mismatched access group can produce real entitlement failures on both simulator and device.

The Most Reliable Workaround: Test on a Real Device

For this specific Xcode 8 plus iOS 10 simulator issue, the most reliable workaround was to test keychain behavior on real hardware. If the app succeeds there, you have strong evidence that the simulator is the noisy variable.

That matters because many developers waste time rewriting correct keychain code to solve what is actually a tooling bug.

Additional Simulator Workarounds

If you must keep working in the simulator, the following sometimes helps:

  • reset the simulator content and settings
  • delete the app from the simulator and reinstall it
  • restart Xcode and the simulator
  • clean the build folder before rerunning

These steps do not fix the underlying platform issue, but they can clear corrupted simulator state.

Example: Delete Before Add During Repeated Testing

Repeated keychain tests can also fail for a different reason: the item already exists. That is a separate status code, but while debugging it helps to make the test idempotent:

swift
1import Security
2
3let query: [String: Any] = [
4    kSecClass as String: kSecClassGenericPassword,
5    kSecAttrAccount as String: "demo-user"
6]
7
8SecItemDelete(query as CFDictionary)
9
10let addQuery: [String: Any] = [
11    kSecClass as String: kSecClassGenericPassword,
12    kSecAttrAccount as String: "demo-user",
13    kSecValueData as String: Data("secret".utf8)
14]
15
16let status = SecItemAdd(addQuery as CFDictionary, nil)
17print(status)

This does not solve -34018, but it removes "duplicate item" confusion while you test.

When It Is a Real Entitlement Problem

Do not overcorrect toward "simulator bug" if the error also appears on a device. In that case, inspect:

  • provisioning profile
  • app signing identity
  • keychain sharing capability
  • access group values in the query
  • whether extensions and host apps use compatible entitlements

If the code touches shared keychain access groups, entitlement mismatches become much more likely.

A Practical Decision Rule

Use this rule of thumb:

  • simulator only: likely Xcode 8 and iOS 10 simulator behavior
  • simulator and device: likely real signing or entitlement configuration problem

That single test saves a lot of time.

Common Pitfalls

  • Assuming -34018 always means your code is wrong, even when the failure is simulator-only.
  • Debugging the keychain query forever without first testing on a physical device.
  • Forgetting that access groups require matching entitlements and provisioning.
  • Mixing old simulator state with fresh builds, which can make the issue look intermittent.
  • Confusing -34018 with duplicate-keychain-item errors that need different handling.

Summary

  • In Xcode 8 with the iOS 10 simulator, SecItemAdd returning -34018 was often a simulator issue rather than a real app bug.
  • The fastest way to separate causes is to test the same code on a real device.
  • You should still verify signing, bundle ID, keychain sharing, and access groups.
  • Resetting simulator state can help, but it is only a workaround.
  • If the error happens on device too, treat it as a genuine entitlement or signing problem.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms