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.
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:
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:
- confirm the target signs correctly
- confirm the bundle identifier is stable
- enable Keychain Sharing in Signing and Capabilities if the app expects it
- 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:
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
-34018always 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
-34018with duplicate-keychain-item errors that need different handling.
Summary
- In Xcode 8 with the iOS 10 simulator,
SecItemAddreturning-34018was 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.
Related reading
- SecItemAdd and SecItemCopyMatching returns error code -34018 errSecMissingEntitlement
- Select all text inside EditText when it gets focus
- Selected tab's color in Bottom Navigation View
- selector in Swift?
- Selector syntax for swift 3.0
- Send and receive messages through NSNotificationCenter in Objective-C?
- Send data from activity to fragment in Android
- Send POST parameters with MultipartFormData using Alamofire, in iOS Swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.