How to prevent choosePrivateKeyAlias dialog in android app?

前端 未结 2 1117
春和景丽
春和景丽 2021-01-14 08:36

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

2条回答
  •  天命终不由人
    2021-01-14 09:10

    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.

提交回复
热议问题