Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 675
Jakub Gruber Avatar asked Oct 16 '17 09:10

Jakub Gruber


Video Answer


1 Answers

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.

like image 145
Max Avatar answered Sep 19 '22 09:09

Max