1# Contacts Kit Development 2 3The Contacts Kit allows users to add, delete, modify, and query contacts easily. It provides a series of APIs for you to quickly integrate contact management functions into your applications. 4 5For details, see [@ohos.contact](../reference/apis-contacts-kit/js-apis-contact.md). 6 7 8## Available Capabilities 9 10Using the Contacts Kit, you can manage contacts, including adding, deleting, modifying, and querying contact information. You can also use the Picker function to open the contact list. 11 12The following capabilities are opened to all applications: 13 14- [Contact Selection with Picker](#contact-selection) 15 16The following capabilities are restrictedly opened to third-party applications: 17 18<!--RP1--> 19> **NOTE** 20> 21> To read contacts, you need to declare the **ohos.permission.READ_CONTACTS** permission. This permission is of the **system_basic** level. To add, delete, or update contacts, you need to declare the **ohos.permission.WRITE_CONTACTS** permission. This permission is of the **system_basic** level. 22<!--RP1End--> 23 24- [Contact Management](#contact-management-restricted-permission) 25 26- [Contact Selection](#contact-selection-restricted-permission) 27 28 29## Contact Selection 30 31When you select a contact, the contact list is displayed in Picker mode to facilitate selection. You do not need to apply for permissions for using the API. 32 331. Import the related modules. 34 35 ```ts 36 import { contact } from '@kit.ContactsKit'; 37 import { BusinessError } from '@kit.BasicServicesKit'; 38 ``` 39 402. Call the contact API to display the contact list, and click the desired contact. 41 42 ```ts 43 contact.selectContacts({ 44 isMultiSelect:false 45 },(err: BusinessError, data) => { 46 if (err) { 47 console.error(`selectContact callback: err->${JSON.stringify(err)}`); 48 return; 49 } 50 console.log(`selectContact callback: success data->${JSON.stringify(data)}`); 51 }); 52 53 ``` 54 553. View the returned contact data. 56 57 58## Contact Selection (Restricted Permission) 59 601. Declare the required permission: 61 <!--RP2--> 62 To select a contact, you need to declare the **ohos.permission.WRITE_CONTACTS** permission to call the **selectContacts** API. This permission is of the **system_basic** level. Before declaring the required permission, ensure that the [basic principles for using permissions](../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. Then, declare the requried permission by referring to [Requesting Application Permissions](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 63 <!--RP2End--> 642. Include an array of required permissions in **Permissions**. 65 663. Perform the corresponding operation on the contact. 67 68 ```ts 69 import { common, abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; 70 import { contact } from '@kit.ContactsKit'; 71 72 let context = getContext(this) as common.UIAbilityContext; 73 const permissions: Array<Permissions> = ['ohos.permission.WRITE_CONTACTS']; 74 75 abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, permissions).then(() => { 76 try { 77 contact.selectContacts(); 78 } catch(err) { 79 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 80 } 81 }) 82 83 ``` 84 85 86## Contact Management (Restricted Permission) 87 88To implement contact management for an application, use the **permissions** API to request for the contact editing permission. 89 901. Declare the required permission: 91 <!--RP2--> 92 - To delete a contact, you need to declare the **ohos.permission.WRITE_CONTACTS** permission to call the **deleteContact** API. This permission is of the **system_basic** level. 93 - To update a contact, you need to declare the **ohos.permission.WRITE_CONTACTS** permission to call the **updateContact** API. This permission is of the **system_basic** level. 94 - To query a contact, you need to declare the **ohos.permission.READ_CONTACTS** permission to call the **queryContact** API. This permission is of the **system_basic** level. 95 Before declaring the required permission, ensure that the [basic principles for using permissions](../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. Then, declare the requried permission by referring to [Requesting Application Permissions](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 96 <!--RP2End--> 97 982. Include an array of required permissions in **Permissions**. 99 1003. Perform the corresponding operation on the contact. 101 102 ```ts 103 // Sample code 104 import { common, abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; 105 import { contact } from '@kit.ContactsKit'; 106 107 @Entry 108 @Component 109 struct Contact { 110 addContactByPermissions() { 111 let context = getContext(this) as common.UIAbilityContext; 112 const permissions: Array<Permissions> = ['ohos.permission.WRITE_CONTACTS']; 113 const contactInfo: contact.Contact = { 114 name: { fullName: 'Wang Xiaoming' }, 115 phoneNumbers: [{ phoneNumber: '13912345678' }] 116 } 117 abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, permissions).then(() => { 118 try { 119 contact.addContact(context, contactInfo, (err, data) => { 120 if (err) { 121 console.log('addContact callback: err->' + JSON.stringify(err)); 122 return; 123 } 124 console.log('addContact callback: data->' + JSON.stringify(data)); 125 }) 126 } catch (err) { 127 console.error('errCode: ' + err.code + ', errMessage: ' + err.message); 128 } 129 }) 130 } 131 132 build() { 133 Row() { 134 Column() { 135 Button ('Add Contact') 136 .onClick(() => { 137 this.addContactByPermissions(); 138 }) 139 } 140 .width('100%') 141 } 142 .height('100%') 143 } 144 } 145``` 146