I have an android app that call a secured website in a webview. The webview retrieve the certificate to give it to the website.
I have to use the KeyChain.choos
I use the following in order to check whether or not the user has already selected the certificate:
activity?.let { it1 ->
var selectedPrivateKey = KeyChain.getPrivateKey(it1, "keystore-test")
if (selectedPrivateKey == null) { // if we can't access the private key then prompt for cert selection
KeyChain.choosePrivateKeyAlias(it1, {
Log.d("App", "Popped up KeyChain selection screen")
}, null, null, null, "keystore-test") // this is the alias that is chosen in the popup by default
}
}
KeyChain.getPrivateKey(it1, "keystore-test") will return null if you don't have access to the private key, meaning that the user did not select a certificate yet.
This implementation will save you from having to use SharedPreferences and also won't give you a false positive if the user deletes the certificate.