How to delete an item from the keychain?

How do I delete an item from the keychain? Both the Xojo and the MBS version fail.

Xojo:

dim theKeyChainItem as new KeyChainItem theKeyChainItem.ServiceName = app.ApplicationNameMBS theKeyChainItem.Label = app.ApplicationNameMBS theKeyChainItem.AccountName = KeyChainName try theKeyChainItem.Delete Return true catch err as KeyChainException globals.theErrorLog.DialogErrorProceed "It wasn't possible to delete the password from the Keychain! Error: " + Str(err.ErrorNumber) + " " + err.Message Return False end try

MBS:

[code] // Build query
dim KeyChainDict as new Dictionary

KeyChainDict.value(KeychainManagerMBS.kSecClass) = KeychainManagerMBS.kSecClassGenericPassword
KeyChainDict.value(KeychainManagerMBS.kSecAttrAccount) = KeyChainName
KeyChainDict.value(KeychainManagerMBS.kSecAttrService) = app.ApplicationNameMBS

// search the item and delete it
if KeychainManagerMBS.DeleteItem(KeyChainDict) then return true[/code]

There is no error message or exception. The keychain item just isn’t deleted. Christian says that the code works for him. But I’ve now tested this on both my computers (10.8 and 10.9, built and debug) with the same result.

Using Xojo2013r3, Cocoa.

[code] Dim EltClefApp as KeyChainItem ’ J’ai gard le mme format de retour qu’avec les IntKey, le premier caractre indique le rsultat de l’opration (si erreur ou etc.)
Dim TampPass as String
Dim DrapClefExists as Boolean

’ TampPass = “#” ’ Sera retourn si erreur
If System.KeyChainCount > 0 Then
EltClefApp = New KeyChainItem
EltClefApp.ServiceName = ClefServNom ’ O = s_Server d’internet key
Try
TampPass = System.KeyChain.FindPassword(EltClefApp)
DrapClefExists= True
Catch
TampPass = “#” ’ Elle est cens exister si on voulait l’effacer
DrapClefExists = False ’ Soit la clef n’existe pas, soit le gars n’a pas autoris l’accs
End Try
’ MsgBox "Clef existe dj : " + Cstr(DrapClefExists)

If DrapClefExists Then
  ' PressPapTxt("O = s_Server : '" + EltClefApp.ServiceName + "'" + EndOfLine + "Compte = a_AccName : '" + EltClefApp.AccountName + "'")
  ' ATTENTION, ne pas faire MsgBox  ServiceName  et  AccountName  car a plante, par contre a marche PARFOIS si on les colle dans le presse-papiers
  ' MsgBox "'" + EltClefApp.ServiceName + "'" ' O = s_Server d'internet key
  ' MsgBox "'" + EltClefApp.AccountName + "'" ' Compte = a_AccName d'internet key
  ' MsgBox "'" + EltClefApp.Label + "'" + EndOfLine + "'" + EltClefApp.Comment + "'" + EndOfLine + "'" + EltClefApp.Description + "'"
  '                   Nom = l_Label                                   Commentaires = j_Comment                                  Type = C_Type ?
  ' MsgBox "On efface la clef"
  Try ' Je met Try si jamais une clef sur laquelle on n'a pas ou plus les droits (cre par une autre app ou changement de version)
    EltClefApp.Delete
    TampPass = "-" ' La clef a t efface
    DrapClefExists = False ' Puisqu'on l'a efface mais on s'en fout on ne s'en sert plus
  Catch
    TampPass = "#"
    '  DrapClefExists  reste  True mais on ne l'utilise plus
  End Try
  ' MsgBox "On a effac la clef" + EndOfLine + "TampPass = " + TampPass
Else
  '   Rien  DrapClefExists  reste  False et  TampPass  reste  "#"
  If not(TampPass = "#") Then MsgBox "Error ! Please contact the authors." + EndOfLine + "EffAppKey" + EndOfLine + "TampPass should be '#' ? " + TampPass
End If

Else
TampPass = “#”
’ Beep
’ MsgBox “You don’t have a key chain.”

End If

Return TampPass[/code]
It works, the difference with you is that I do

TampPass = System.KeyChain.FindPassword(EltClefApp)

I check if the Keychain exists. Maybe we can’t remove a Keychain without read its password ?

Thanks! That was it.