Cannot use Keychain in Xcode9 tests
Asked Answered
P

1

8

I have a few unit tests to verify that the way I work with Keychain is correct and data are in same form when I load them.

Tests were running ok until update to XCode 9. At the moment, KeychainService return -50 (not saved).

According to this question, it was solved by adding Host to unit tests. However, my tests are in framework project and there is no app to use as a host.

let query = [
    kSecClass as String       : kSecClassGenericPassword as String,
    kSecAttrAccount as String : key,
    kSecValueData as String   : data ] as [String : Any]

SecItemDelete(query as CFDictionary)

SecItemAdd(query as CFDictionary, nil)

What is recommended solution? I expect just some configuration in XCode, moving tests into app is not a proper solution for me.

Phoenix answered 16/10, 2017 at 9:13 Comment(3)
any related code-snippet...? I have a perfectly functioning KeyChain access in Xcode9, Swift4; no anomalies have been experienced here. you may also take a look on the official GenericKeychain project at Apple's site for comparing to yours and finding the glitch.Kiri
I added code sample. Calling SecItemAdd return -50Phoenix
To clarify - KeyChain is working, but not in unit testsPhoenix
U
14

Starting with Xcode 9, test bundles require a host app to access the keychain from within the iOS simulator, see https://mcmap.net/q/1325388/-reading-from-keychain-xcode-9-simulators-returns-nothing.

Simply add an app target and set it as the host application for your unit test bundle.

Upas answered 20/10, 2017 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.