• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using the Picker for Contact Management
2
3<!--Kit: Contacts Kit-->
4<!--Subsystem: Applications-->
5<!--Owner: @librahCode-->
6<!--Designer: @yanghaoqian-->
7<!--Tester: @shangzhijie-->
8<!--Adviser: @zhang_yixin13-->
9## Available APIs
10
11| API                 | Description                                      |
12| --------------------- | ------------------------------------------ |
13| addContactViaUI(context: Context, contact: Contact): Promise&lt;number&gt; | Opens the **Add contact** page to add a contact. This API uses a promise to return the result.|
14| saveToExistingContactViaUI(context: Context, contact: Contact): Promise&lt;number&gt; | Opens the **Save to existing** page to save a contact to an existing one. This API uses a promise to return the result.|
15
16
17## Creating a Contact
18
19Call **addContactViaUI** to launch the UI for contact creation. Then, uses can complete contact information on the UI.
20
21```js
22import { common } from '@kit.AbilityKit';
23import { contact } from '@kit.ContactsKit';
24import { BusinessError } from '@kit.BasicServicesKit';
25
26
27@Entry
28@Component
29struct Index {
30  @State message: string = 'Hello World';
31
32  build() {
33    Column() {
34      Text(this.message)
35        .fontSize(50)
36        .fontWeight(FontWeight.Bold)
37        .onClick(() => {
38          let contactInfo: contact.Contact = {
39            name: {
40              fullName: 'xxx'
41            },
42            phoneNumbers: [{
43              phoneNumber: '138xxxxxx'
44            }]
45          }
46          let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
47          let promise = contact.addContactViaUI(context, contactInfo);
48          promise.then((data) => {
49              console.info(`Succeeded in add Contact via UI.data->${JSON.stringify(data)}`);
50            }).catch((err: BusinessError) => {
51              console.error(`Failed to add Contact via UI. Code: ${err.code}, message: ${err.message}`);
52            });
53        })
54    }
55  }
56}
57```
58
59## Updating Contact Information
60
61Call **saveToExistingContactViaUI** to launch the UI for contact information updating. Then, users can update information about a specific contact on the UI.
62
63```js
64import { common } from '@kit.AbilityKit';
65import { contact } from '@kit.ContactsKit';
66import { BusinessError } from '@kit.BasicServicesKit';
67
68
69@Entry
70@Component
71struct Index {
72  @State message: string = 'Hello World';
73
74  build() {
75    Column() {
76      Text(this.message)
77        .fontSize(50)
78        .fontWeight(FontWeight.Bold)
79        .onClick(() => {
80          let contactInfo: contact.Contact = {
81            id: 1,
82            name: {
83              fullName: 'xxx'
84            },
85            phoneNumbers: [{
86              phoneNumber: '138xxxxxx'
87            }]
88          }
89          let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
90          let promise = contact.saveToExistingContactViaUI(context, contactInfo);
91          promise.then((data) => {
92              console.info(`Succeeded in save to existing Contact via UI.data->${JSON.stringify(data)}`);
93            }).catch((err: BusinessError) => {
94              console.error(`Failed to save to existing Contact via UI. Code: ${err.code}, message: ${err.message}`);
95            });
96        })
97    }
98  }
99}
100```
101