Jakub Gruber
Jakub Gruber

Reputation: 785

Cannot use Keychain in Xcode9 tests

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.

Upvotes: 8

Views: 4308

Answers (1)

Max
Max

Reputation: 1552

Starting with Xcode 9, test bundles require a host app to access the keychain from within the iOS simulator, see https://stackoverflow.com/a/46317131/5082444.

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

Upvotes: 14

Related Questions