Difference between revisions of "KeychainItem"

From Xojo Documentation

 
Line 41: Line 41:
 
     System.Keychain.AddPassword(newItem, "SecretPassword")
 
     System.Keychain.AddPassword(newItem, "SecretPassword")
 
   Catch Exception error As KeychainException
 
   Catch Exception error As KeychainException
     MessageDialog.Show("Can't add item: " + error.Message)
+
     MessageBox("Can't add item: " + error.Message)
 
   End Try
 
   End Try
 
Else
 
Else
   MessageDialog.Show("You don't have a key chain.")
+
   MessageBox("You don't have a key chain.")
 
End If
 
End If
 
</rbcode>
 
</rbcode>
Line 61: Line 61:
 
   // get application's password from the system keychain
 
   // get application's password from the system keychain
 
   password = System.Keychain.FindPassword(itemToFind)
 
   password = System.Keychain.FindPassword(itemToFind)
   MessageDialog.Show("The password for this item is: " + password)
+
   MessageBox("The password for this item is: " + password)
 
Catch Exception error As KeychainException
 
Catch Exception error As KeychainException
   MessageDialog.Show("Can't find item: " + error.Message)
+
   MessageBox("Can't find item: " + error.Message)
 
End Try
 
End Try
 
</rbcode>
 
</rbcode>

Latest revision as of 23:27, 29 August 2019

Class (inherits from Object)


Refers to a macOS Keychain item.


Properties
AccountName Description Label
Comment Handle fa-lock-32.png ServiceName


Methods
Remove


Notes

KeychainItems can access passwords for applications only, not internet passwords.

Examples

The following example adds a KeychainItem for an application and assigns a password.

Var newItem As KeychainItem
If System.KeychainCount > 0 Then
newItem = New KeychainItem
// Indicate the name of the application
newItem.ServiceName = "MyApplication"

Try
// Create a new keychain item for the application and assign the password
System.Keychain.AddPassword(newItem, "SecretPassword")
Catch Exception error As KeychainException
MessageBox("Can't add item: " + error.Message)
End Try
Else
MessageBox("You don't have a key chain.")
End If

The following example retrieves the password and displays it in a message box.

Var itemToFind As KeychainItem
Var password As String

itemToFind = New KeychainItem
// Indicate the name of the application whose keychain item you wish to find
itemToFind.ServiceName = "MyApplication"

Try
// get application's password from the system keychain
password = System.Keychain.FindPassword(itemToFind)
MessageBox("The password for this item is: " + password)
Catch Exception error As KeychainException
MessageBox("Can't find item: " + error.Message)
End Try

See Also

Keychain class; KeychainException error; System module.