|
| 1 | +# Contact |
| 2 | + |
| 3 | +The `Contact` module in the Scripting app allows you to access and manage contacts on the device. You can create, query, update, and delete contacts, as well as manage contact groups and containers. |
| 4 | + |
| 5 | +## Overview of Data Structures |
| 6 | + |
| 7 | +| Type | Description | |
| 8 | +| -------------------------------- | ---------------------------------------------------------------------------- | |
| 9 | +| **ContactInfo** | Represents detailed information of a single contact. | |
| 10 | +| **ContactContainer** | Represents a contact storage container, such as local, Exchange, or CardDAV. | |
| 11 | +| **ContactGroup** | Represents a contact group for categorizing contacts. | |
| 12 | +| **ContactLabeledValue** | A labeled value, such as phone number or email address. | |
| 13 | +| **ContactPostalAddress** | Represents a postal address. | |
| 14 | +| **ContactSocialProfile** | Represents social profile information. | |
| 15 | +| **ContactInstantMessageAddress** | Represents instant messaging account information. | |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Creating a Contact |
| 20 | + |
| 21 | +```ts |
| 22 | +try { |
| 23 | + const contact = await Contact.createContact({ |
| 24 | + givenName: "John", |
| 25 | + familyName: "Doe", |
| 26 | + phoneNumbers: [{ label: "mobile", value: "+1234567890" }], |
| 27 | + emailAddresses: [{ label: "work", value: "john.doe@example.com" }], |
| 28 | + }); |
| 29 | + console.log("Contact created:", contact); |
| 30 | +} catch (error) { |
| 31 | + console.error("Failed to create contact:", error); |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +- Either `givenName` or `familyName` is required. |
| 36 | +- You may specify an optional `containerIdentifier`. If not provided, the contact is added to the default container. |
| 37 | +- Always handle potential errors due to permission issues or invalid input. |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Updating a Contact |
| 42 | + |
| 43 | +```ts |
| 44 | +try { |
| 45 | + const updated = await Contact.updateContact({ |
| 46 | + identifier: contact.identifier, |
| 47 | + phoneNumbers: [{ label: "home", value: "+9876543210" }], |
| 48 | + }); |
| 49 | + console.log("Contact updated:", updated); |
| 50 | +} catch (error) { |
| 51 | + console.error("Failed to update contact:", error); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +- `identifier` is required. |
| 56 | +- Only the provided fields will be updated; others remain unchanged. |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Fetching Contacts |
| 61 | + |
| 62 | +### Fetch a Contact by Identifier |
| 63 | + |
| 64 | +```ts |
| 65 | +try { |
| 66 | + const contact = await Contact.fetchContact(contactId, { fetchImageData: true }); |
| 67 | + console.log("Contact fetched:", contact); |
| 68 | +} catch (error) { |
| 69 | + console.error("Failed to fetch contact:", error); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Fetch All Contacts |
| 74 | + |
| 75 | +```ts |
| 76 | +try { |
| 77 | + const contacts = await Contact.fetchAllContacts({ fetchImageData: false }); |
| 78 | + console.log("All contacts:", contacts); |
| 79 | +} catch (error) { |
| 80 | + console.error("Failed to fetch contacts:", error); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Fetch Contacts in a Container or Group |
| 85 | + |
| 86 | +```ts |
| 87 | +try { |
| 88 | + const contacts = await Contact.fetchContactsInContainer(containerId); |
| 89 | + console.log("Contacts in container:", contacts); |
| 90 | +} catch (error) { |
| 91 | + console.error("Failed to fetch contacts in container:", error); |
| 92 | +} |
| 93 | + |
| 94 | +try { |
| 95 | + const groupContacts = await Contact.fetchContactsInGroup(groupId); |
| 96 | + console.log("Contacts in group:", groupContacts); |
| 97 | +} catch (error) { |
| 98 | + console.error("Failed to fetch contacts in group:", error); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +- Set `fetchImageData` to `true` only if you need the contact's image data. |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## Deleting a Contact |
| 107 | + |
| 108 | +```ts |
| 109 | +try { |
| 110 | + await Contact.deleteContact(contactId); |
| 111 | + console.log("Contact deleted"); |
| 112 | +} catch (error) { |
| 113 | + console.error("Failed to delete contact:", error); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +## Contact Container Management |
| 120 | + |
| 121 | +### Fetch All Containers |
| 122 | + |
| 123 | +```ts |
| 124 | +try { |
| 125 | + const containers = await Contact.fetchContainers(); |
| 126 | + console.log("Contact containers:", containers); |
| 127 | +} catch (error) { |
| 128 | + console.error("Failed to fetch containers:", error); |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Get the Default Container Identifier |
| 133 | + |
| 134 | +```ts |
| 135 | +try { |
| 136 | + const defaultContainerId = await Contact.defaultContainerIdentifier; |
| 137 | + console.log("Default container ID:", defaultContainerId); |
| 138 | +} catch (error) { |
| 139 | + console.error("Failed to fetch default container:", error); |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +Container types: |
| 144 | + |
| 145 | +- `unassigned` |
| 146 | +- `local` |
| 147 | +- `exchange` |
| 148 | +- `cardDAV` |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## Contact Group Management |
| 153 | + |
| 154 | +### Create a Contact Group |
| 155 | + |
| 156 | +```ts |
| 157 | +try { |
| 158 | + const group = await Contact.createGroup("Friends", defaultContainerId); |
| 159 | + console.log("Group created:", group); |
| 160 | +} catch (error) { |
| 161 | + console.error("Failed to create group:", error); |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +### Fetch Groups |
| 166 | + |
| 167 | +```ts |
| 168 | +try { |
| 169 | + const groups = await Contact.fetchGroups(); |
| 170 | + console.log("Groups:", groups); |
| 171 | +} catch (error) { |
| 172 | + console.error("Failed to fetch groups:", error); |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +### Delete a Group |
| 177 | + |
| 178 | +```ts |
| 179 | +try { |
| 180 | + await Contact.deleteGroup(groupId); |
| 181 | + console.log("Group deleted"); |
| 182 | +} catch (error) { |
| 183 | + console.error("Failed to delete group:", error); |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## Managing Contact and Group Relationship |
| 190 | + |
| 191 | +### Add Contact to a Group |
| 192 | + |
| 193 | +```ts |
| 194 | +try { |
| 195 | + await Contact.addContactToGroup(contactId, groupId); |
| 196 | + console.log("Contact added to group"); |
| 197 | +} catch (error) { |
| 198 | + console.error("Failed to add contact to group:", error); |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +### Remove Contact from a Group |
| 203 | + |
| 204 | +```ts |
| 205 | +try { |
| 206 | + await Contact.removeContactFromGroup(contactId, groupId); |
| 207 | + console.log("Contact removed from group"); |
| 208 | +} catch (error) { |
| 209 | + console.error("Failed to remove contact from group:", error); |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +--- |
| 214 | + |
| 215 | +## Example ContactInfo Structure |
| 216 | + |
| 217 | +```json |
| 218 | +{ |
| 219 | + "identifier": "XXXX-XXXX", |
| 220 | + "givenName": "John", |
| 221 | + "familyName": "Doe", |
| 222 | + "phoneNumbers": [{ "label": "mobile", "value": "+1234567890" }], |
| 223 | + "emailAddresses": [{ "label": "work", "value": "john@example.com" }], |
| 224 | + "postalAddresses": [ |
| 225 | + { |
| 226 | + "label": "home", |
| 227 | + "street": "123 Apple St.", |
| 228 | + "city": "Cupertino", |
| 229 | + "state": "CA", |
| 230 | + "postalCode": "95014", |
| 231 | + "country": "USA", |
| 232 | + "isoCountryCode": "US" |
| 233 | + } |
| 234 | + ] |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +## Important Notes |
| 241 | + |
| 242 | +- All API operations can fail due to reasons such as lack of permission or invalid parameters. Always use `try-catch`. |
| 243 | +- User permission is required to access contacts. |
| 244 | +- `imageData` should only be fetched if necessary to reduce memory usage. |
| 245 | +- Ensure the `identifier` is valid when performing update or delete operations. |
| 246 | + |
| 247 | +--- |
| 248 | + |
| 249 | +## Complete Example: Create and Fetch a Contact |
| 250 | + |
| 251 | +```ts |
| 252 | +try { |
| 253 | + const contact = await Contact.createContact({ |
| 254 | + givenName: "Alice", |
| 255 | + familyName: "Smith", |
| 256 | + phoneNumbers: [{ label: "mobile", value: "+19876543210" }], |
| 257 | + }); |
| 258 | + console.log("Contact created:", contact); |
| 259 | + |
| 260 | + const fetched = await Contact.fetchContact(contact.identifier); |
| 261 | + console.log("Fetched contact:", fetched.givenName); |
| 262 | +} catch (error) { |
| 263 | + console.error("Operation failed:", error); |
| 264 | +} |
| 265 | +``` |
0 commit comments