Run the XCTest below on a Mac whose signing identity matches the keychain-access-groups entitlement you are asserting: it writes, reads and deletes a test secret in that access group and fails on any non-zero OSStatus. A provisioning profile signed before you added an access group breaks Keychain calls at runtime: SecItemAdd returns an error instead of errSecSuccess, and users lose saved credentials. The earlier CI catches that, the cheaper the fix. The authoritative constants are in Apple's Keychain Services reference and the Security framework result codes list.
How do keychain-access-groups and the provisioning profile line up?
The keychain-access-groups entitlement is an array of strings, each prefixed with your Team ID, naming the access groups your app may read and write. Apple's signing server embeds the same list in the provisioning profile. If the two drift, for example you added a group in .entitlements but reused last week's profile, Keychain calls fail at runtime even though the binary links, launches and passes UI tests.
| Mismatch | Symptom at runtime | Root cause |
|---|---|---|
| Group in entitlements, missing from profile | errSecMissingEntitlement on SecItemAdd | Profile not regenerated after adding the group |
| Group in profile, missing from entitlements | No error, items unreachable in that group | Entitlement file drifted from the portal |
| Wrong Team ID prefix | errSecParam (−50) | Hand-edited entitlement without matching prefix |
| Extension entitlement missing the shared group | Extension writes, container app can't read | Extension target has its own entitlements file |
Add groups through Signing & Capabilities → + Capability → Keychain Sharing in Xcode so the entitlement file and the profile regenerate together. Hand-editing the XML is the most common way this test starts failing.
What does the keychain test helper look like?
Add this to your test target. It wraps the three SecItem calls and returns a Codable result you can assert on and attach.
// KeychainTestHelper.swift (test target only)
import XCTest
import Security
struct KeychainTestResult: Codable {
let accessGroup: String
let writeStatus: Int32
let readStatus: Int32
let deleteStatus: Int32
var allPassed: Bool {
writeStatus == errSecSuccess &&
readStatus == errSecSuccess &&
deleteStatus == errSecSuccess
}
}
struct KeychainTestHelper {
/// Exercises a full write/read/delete cycle in `accessGroup`.
static func runCycle(accessGroup: String, label: String = "KeychainTestHelper") -> KeychainTestResult {
let testAccount = "xctest-\(UUID().uuidString)"
let testService = "com.figment.keychain-test"
guard let testData = "figment-ci-test-secret".data(using: .utf8) else {
fatalError("UTF-8 encoding failed")
}
let addQuery: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: testService,
kSecAttrAccount: testAccount,
kSecAttrAccessGroup: accessGroup,
kSecValueData: testData,
kSecAttrLabel: label,
]
let writeStatus = SecItemAdd(addQuery as CFDictionary, nil)
let readQuery: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: testService,
kSecAttrAccount: testAccount,
kSecAttrAccessGroup: accessGroup,
kSecReturnData: true,
]
var readResult: AnyObject?
let readStatus = SecItemCopyMatching(readQuery as CFDictionary, &readResult)
let deleteQuery: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: testService,
kSecAttrAccount: testAccount,
kSecAttrAccessGroup: accessGroup,
]
let deleteStatus = SecItemDelete(deleteQuery as CFDictionary)
return KeychainTestResult(
accessGroup: accessGroup,
writeStatus: writeStatus,
readStatus: readStatus,
deleteStatus: deleteStatus
)
}
}
How do I write the XCTest and the JSON attachment?
Call the helper from a test, clean up leftovers from crashed runs first, and attach the result as JSON.
// KeychainEntitlementTests.swift
import XCTest
final class KeychainEntitlementTests: XCTestCase {
// Replace with your real Team ID prefix and group — must match .entitlements exactly.
let accessGroup = "ABCDE12345.com.example.MyApp.shared"
func testKeychainCycleSucceeds() throws {
// Clean up anything a previous crashed run left behind.
let cleanup: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: "com.figment.keychain-test",
kSecAttrAccessGroup: accessGroup,
]
SecItemDelete(cleanup as CFDictionary)
let result = KeychainTestHelper.runCycle(accessGroup: accessGroup)
attachResult(result)
XCTAssertEqual(result.writeStatus, errSecSuccess, "SecItemAdd returned \(result.writeStatus)")
XCTAssertEqual(result.readStatus, errSecSuccess, "SecItemCopyMatching returned \(result.readStatus)")
XCTAssertEqual(result.deleteStatus, errSecSuccess, "SecItemDelete returned \(result.deleteStatus)")
}
private func attachResult(_ result: KeychainTestResult) {
guard let jsonData = try? JSONEncoder().encode(result) else { return }
let attachment = XCTAttachment(data: jsonData, uniformTypeIdentifier: "public.json")
attachment.name = "keychain-test-result.json"
attachment.lifetime = .keepAlways
add(attachment)
}
}
This code is an example, not a reported benchmark. Run it against your own project and signing identity. After the run, open Xcode → Report Navigator, pick the test, and the attachment shows up under the test name:
{
"accessGroup": "ABCDE12345.com.example.MyApp.shared",
"writeStatus": 0,
"readStatus": 0,
"deleteStatus": 0
}
allPassed is a Swift-side convenience derived from the three status fields. It is not encoded into the JSON.
Which return codes will I see?
Mostly the five below. Numeric values track the Security framework result-codes page linked above, so confirm them against the header for your SDK.
| OSStatus | Constant | What it means here |
|---|---|---|
| 0 | errSecSuccess | Call succeeded |
| −34018 | errSecMissingEntitlement | Access group isn't in the signed entitlements (regenerate the profile) |
| −25299 | errSecDuplicateItem | A prior run died before delete; the cleanup step above clears this |
| −25300 | errSecItemNotFound | Nothing to read/delete; treat as failure if you expected a write first |
| −50 | errSecParam | Malformed query; usually a wrong kSecAttrAccessGroup string or missing Team ID prefix |
Why does the Simulator pass when a device fails?
The iOS Simulator does not enforce Keychain access-group codesigning the way a real device or signed macOS binary does. SecItemAdd can return errSecSuccess on the Simulator even when the provisioning profile lacks the group. Tests that pass on Simulator but fail on device nearly always come down to this. Running the XCTest against a signed Mac target, with a Development or Developer ID certificate installed in the login keychain, exercises the same enforcement your App Store Connect build will see.
Can any plan run the keychain entitlement suite?
Any plan with SSH or remote desktop can run this suite, because it needs only Xcode, xcodebuild and your signing certificate in the login keychain. The Intel Mac VM ($25 a month, $2 a day) runs macOS Sequoia with Xcode 26.3 or earlier. ARM-only binaries, Xcode 26.4 or later and visionOS targets need an Apple silicon plan. We have not run this test on any plan.
| Plan | Monthly | 12 months | Fits when |
|---|---|---|---|
| Intel Mac VM | $25 | $300 | Your project supports Xcode 26.3 or earlier |
| Apple Silicon Mac VM | $39 | $468 | You need arm64 execution or Xcode 26.4 or later |
| Dedicated M4 Pro 48 GB | $349 | $3,228 paid up front | You want a whole Mac for signing and CI |
Arithmetic: 12 × $25 = $300; 12 × $39 = $468. The Dedicated M4 Pro 48 GB is $3,228 for 12 months paid up front, or 12 × $299 = $3,588 on a 12-month term. You manage the runner, keychain setup and artifact retention yourself. See the Mac hosting plans.