Difference between revisions of "KeychainItem"

From Xojo Documentation

m (1 revision)
 
 
(21 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 +
{{ClassBox
 +
| super=[[Object]]
 +
| platform=mac
 +
| scope=global
 +
}}
 +
{{Description
 +
|text = Refers to a macOS [[Keychain|Keychain]] item. }}
  
  
=='''Description'''==
 
Refers to a Macintosh [[KeyChain|Keychain]] item.
 
  
 +
<dynamicTable id="Properties" class="propertyTable" title="Properties" columns="3">
 +
{{Property | name=AccountName | type=String | platform=all | description=( String)&nbsp;&nbsp;Contains the name of the account (required for adding, can be Nil to find). }}
 +
{{Property | name=Comment | type=String | platform=all | description=( String)&nbsp;&nbsp;End user editable string containing comments for this Keychain item. }}
 +
{{Property | name=Description | type=String | platform=all | description=( String)&nbsp;&nbsp;End-user visible string describing this Keychain item. }}
 +
{{Property | name=Handle | type=Integer | platform=all | readonly=yes | description=( Integer)&nbsp;&nbsp;Contains the KeychainItem reference, for use with Macintosh toolbox calls. }}
 +
{{Property | name=Label | type=String | platform=all | description=( String)&nbsp;&nbsp;End-user editable string containing the label for this Keychain item. }}
 +
{{Property | name=ServiceName | type=String | platform=all | description=( String)&nbsp;&nbsp;Contains the name of the service (required for adding, can be Nil to find).  }}
 +
</dynamicTable>
  
  
=='''Super Class'''==
+
<dynamicTable id="Methods" class="methodTable" title="Methods" columns="2">
[[Object|Object]]
+
{{Method | name=Remove | description=Remove()&#x0A;Removes the KeychainItem.  }}
 +
</dynamicTable>
  
=='''Properties'''==
 
  
{| cellpadding="8" cellspacing="0" border="1"
+
==Notes==
 +
'''KeychainItems''' can access passwords for applications only, not internet passwords.
  
! width=10%  style="background-color:#e0e0e0" | Name
+
==Examples==
 +
The following example adds a [[KeychainItem]] for an application and assigns a password.
  
! width=15%  style="background-color:#e0e0e0" | Type
+
<rbcode>
 +
Var newItem As KeychainItem
 +
If System.KeychainCount > 0 Then
 +
  newItem = New KeychainItem
 +
  // Indicate the name of the application
 +
  newItem.ServiceName = "MyApplication"
  
! width=55%  style="background-color:#e0e0e0" | Description
+
  Try
|-
+
    // Create a new keychain item for the application and assign the password
|AccountName
+
    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
 +
</rbcode>
  
|[[String|String]]
+
The following example retrieves the password and displays it in a message box.
|Contains the name of the account (required for adding, can be [[Nil|Nil]] to find).
 
  
|-
+
<rbcode>
|Comment
+
Var itemToFind As KeychainItem
 +
Var password As String
  
|[[String|String]]
+
itemToFind = New KeychainItem
|End user editable string containing comments for this '''Keychain''' item.
+
// Indicate the name of the application whose keychain item you wish to find
 +
itemToFind.ServiceName = "MyApplication"
  
|-
+
Try
|Description
+
  // 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
 +
</rbcode>
  
|[[String|String]]
+
==See Also==
|End-user visible string describing this Keychain item.
+
[[Keychain]] class; [[KeychainException]] error; [[System]] module.
  
|-
+
[[Category:Language_Classes]]
|<div style="font-weight:bold; color:red;">Handle</div>
+
[[Category:Macintosh_Keychains]]
|[[Integer|Integer]]
 
|Contains the '''KeyChainItem''' reference, for use with Macintosh toolbox calls.
 
 
 
|-
 
|Label
 
 
 
|[[String|String]]
 
|End-user editable string containing the label for this Keychain item.
 
 
 
|-
 
|ServiceName
 
 
 
|[[String|String]]
 
|Contains the name of the service (required for adding, can be [[Nil|Nil]] to find).
 
To add a password for an application, set ServiceName to the application's name.
 
 
 
|-
 
|}
 
 
 
 
 
=='''Methods'''==
 
 
 
{| cellpadding="8" cellspacing="0" border="1"
 
 
 
! width=10%  style="background-color:#e0e0e0" | Name
 
 
 
! width=20%  style="background-color:#e0e0e0" | Parameters
 
 
 
! width=55%  style="background-color:#e0e0e0" | Description
 
|-
 
|Delete
 
<div style="font-style:italic; color:green;">Introduced 5.2</div>
 
|
 
|Deletes the '''KeyChainItem'''.
 
You can change passwords in a [[KeyChain|KeyChain]] by deleting the original item using this method and adding another item.
 
 
 
|-
 
|}
 
 
 
 
 
=='''Notes'''==
 
REALbasic '''KeyChainItems''' can access passwords for applications only, not internet passwords.
 
 
 
 
 
 
 
=='''Examples'''==
 
The following example adds a [[KeyChainItem|KeyChainItem]] for an application and assigns a password:
 
{| cellpadding="8" cellspacing="0" border="1"
 
|<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] NewItem as [[KeyChainItem|KeyChainItem]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">  [[If|If]] [[System|System]].KeyChainCount &gt; 0 then
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">    
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;  NewItem = [[New|New]] [[KeyChainItem|KeyChainItem]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;  'Indicate the name of the application
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;  NewItem.ServiceName = "MyApplication"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">    
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;  'Create a new keychain item for the application and assign the password
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">    [[System|System]].KeyChain.AddPassword NewItem, "SecretPassword"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;Else
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">    [[Beep|Beep]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">    [[MsgBox|MsgBox]] "You don't have a key chain."
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;End if
 
<br /></div>
 
<br />
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Exception|Exception]] err as [[KeyChainException|KeyChainException]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">  [[MsgBox|MsgBox]] "Can't add item: " + err.Message
 
<br /></div>
 
 
 
|-
 
|}
 
 
 
The following example retrieves the password that was set in the previous example and displays it in a message box.
 
{| cellpadding="8" cellspacing="0" border="1"
 
|<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] ItemToFind as [[KeyChainItem|KeyChainItem]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] password As [[String|String]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">  
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">ItemToFind = [[New|New]] [[KeyChainItem|KeyChainItem]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">  
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'Indicate the name of the application whose keychain item you wish to find
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">ItemToFind.ServiceName = "MyApplication"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">  
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;'get application's password from the system keychain
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">password = [[System|System]].KeyChain.FindPassword(ItemToFind)
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[MsgBox|MsgBox]] "The password for this item is: " + password
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">  
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Exception|Exception]] err as [[KeyChainException|KeyChainException]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[MsgBox|MsgBox]] "Can't find item: " + err.Message
 
<br /></div>
 
 
 
|-
 
|}
 
 
 
 
 
=='''See Also'''==
 
[[KeyChain|KeyChain]] class; [[KeyChainException|KeyChainException]] error; [[System|System]] module.
 
 
 
 
 
[[Category:Classes]]
 
[[Category:Keychains]]
 

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.