Keychain

From Xojo Documentation

Class (inherits from Object)


Gives you access to the default macOS Keychains for your applications. It is a Macintosh-only feature. The Keychain class does not provide access to internet passwords.

Properties
Handle fa-lock-32.png


Methods
AddPassword Lock
FindPassword Unlock


Constructors

Constructor(index as Integer)


Notes

The keychain is a system-wide facility on macOS to store account passwords for applications. By taking advantage of the built-in keychain facility, your users won’t have to type their password if their keychain is unlocked. You should always ask the user before storing something in the keychain.

An equivalent technology to the Mac OS Keychain doesn’t currently exist on other platforms, so the Keychain class is supported only on Macintosh.

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 error As KeychainException
MessageBox("Can't add item: " + error.Message)
End Try
Else
Beep
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

KeychainItem class; KeychainException error; System module.