You need to delete the existing item from the keychain.
This is my sample code, using Apple sample code.
You can get sample code from Apple
Apple recommend you get the state of the credentials before use to check for revoke and if that has occurred to delete any local information
struct KeychainItem {
init(service: String, account: String, accessGroup: String? = nil) {
self.service = service
self.account = account
self.accessGroup = accessGroup
}
static func deleteUserIdentifierFromKeychain() {
do { //please change service id to your bundle ID
try KeychainItem(service: "com.example.apple-samplecode", account: "userIdentifier").deleteItem()
} catch {
print("Unable to delete userIdentifier from keychain")
}
}
func deleteItem() throws {
// Delete the existing item from the keychain.
let query = KeychainItem.keychainQuery(withService: service, account: account, accessGroup: accessGroup)
let status = SecItemDelete(query as CFDictionary)
// Throw an error if an unexpected status was returned.
guard status == noErr || status == errSecItemNotFound else { throw KeychainError.unhandledError }
}
keychainQuery
keychainQuery is from apple sample code.
private static func keychainQuery(withService service: String, account: String? = nil, accessGroup: String? = nil) -> [String: AnyObject] {
var query = [String: AnyObject]()
query[kSecClass as String] = kSecClassGenericPassword
query[kSecAttrService as String] = service as AnyObject?
if let account = account {
query[kSecAttrAccount as String] = account as AnyObject?
}
if let accessGroup = accessGroup {
query[kSecAttrAccessGroup as String] = accessGroup as AnyObject?
}
return query
}