目录

Contacts Service

Contacts Kit allows you 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 under development.

For details, see @ohos.contact (Contacts).

Contacts Selection

When you select a contact, the contact list is displayed in Picker mode to facilitate selection. You do not need to request permissions for using the API.

  1. Import the related modules.

    import contact from '@ohos.contact';
    import { BusinessError } from '@ohos.base';
  2. Call the contact API to display the contact list, and click the desired contact.

    contact.selectContacts({
      isMultiSelect:false
    },(err: BusinessError, data) => {
        if (err) {
          console.error(`selectContact callback: err->${JSON.stringify(err)}`);
            return;
        }
        console.log(`selectContact callback: success data->${JSON.stringify(data)}`);
    });
  3. View the returned data.

Contacts Management

To implement contacts management for an application, use the permissions API to request the contact editing permission.

  1. Include an array of required permissions in Permissions.

  2. Perform the corresponding operation on the contact.

import common from '@ohos.app.ability.common';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import contact from '@ohos.contact';

@Entry
@Component
struct Contact {
  addContactByPermissions() {
    let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
    const permissions: Array<Permissions> = ['ohos.permission.WRITE_CONTACTS'];
    const contactInfo: contact.Contact = {
      name: { fullName: 'Wang Xiaoming' },
      phoneNumbers: [{ phoneNumber: '13912345678' }]
    }
    abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, permissions).then(() => {
      try {
        contact.addContact(context, contactInfo, (err, data) => {
          if (err) {
            console.log('addContact callback: err->' + JSON.stringify(err));
            return;
          }
          console.log('addContact callback: data->' + JSON.stringify(data));
        })
      } catch (err) {
        console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
      }
    })
  }

  build() {
    Row() {
      Column() {
        Button ('Add Contact')
          .onClick(() => {
            this.addContactByPermissions();
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}