For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/angular-lynx/media/contacts.md.

LynxContacts

Picks a contact from the person's address book, or reads it. import { LynxContacts } from '@blotch/angular-lynx';

Use it to invite a friend, split a bill, fill in a delivery contact, or find friends who already use the app.

There are two ways in:

  • pick() opens the system's contact picker. The person chooses, and the app gets only what they chose. It needs no permission and no option, so every app has it. Use it whenever the person picks.
  • getAll() reads every contact the person lets the app read. It needs their permission and permissions.contacts in angular.json.

Turn it on

pick() needs no setting: every app has it. Only reading the address book with getAll() needs one.

Add permissions.contacts to the build target's options in angular.json, with the reason iOS shows when it asks:

angular.json
"options": {
  "permissions": { "contacts": "Finds friends who already use the app" }
}

What it adds:

  • iOS: NSContactsUsageDescription, the reason iOS's prompt shows.
  • Android: the READ_CONTACTS permission, which Android asks the person for the first time the app reads.

Without it, the code that reads the address book is left out of both apps. App Store Connect would flag an app that has it without the reason. permission() is then unsupported, and getAll() rejects with unavailable. The picker is still there.

See Turn on device features for every setting.

Pick a contact

import { Component, inject, signal } from '@angular/core';
import {
  ContactsError,
  LYNX_ELEMENTS,
  LynxContacts,
} from '@blotch/angular-lynx';

@Component({
  selector: 'app-invite',
  imports: [LYNX_ELEMENTS],
  template: `
    <view (bindtap)="invite()"><text>Invite a friend</text></view>
    <text>{{ status() }}</text>
  `,
})
export class Invite {
  // Injected in a field: a method has no injection context.
  readonly #contacts = inject(LynxContacts);
  readonly status = signal('');

  async invite() {
    try {
      // The person picks one of a contact's phone numbers.
      const contact = await this.#contacts.pick({ select: 'phone' });
      if (!contact) return; // They closed the picker.
      this.status.set(`Inviting ${contact.name} at ${contact.phones[0]}`);
    } catch (error) {
      if (error instanceof ContactsError) this.status.set(error.message);
    }
  }
}

pick() resolves with a Contact, or null when the person closes the picker. What the person picks is set by select:

selectThe person picksThe contact comes with
'phone'one of a contact's numbersits id, its name and that number in phones, on every platform
'email'one email addressits id, its name and that address in emails, on every platform
'contact' (default)a whole contactiOS: every phone, email and postal address. Android: its id and name, with empty lists, unless the app may read the address book (below)

To get a number or an address, ask for it with 'phone' or 'email'. That's the same everywhere, and needs nothing.

Android's picker hands an app only the contact's name when it picks a whole contact. The rest of the contact is in the address book, which needs READ_CONTACTS. So an app with permissions.contacts, whose person allowed it (permission() is granted), gets every field on Android too.

The picker never asks for a permission. On iOS it's ContactsUI's CNContactPickerViewController, which runs outside the app. On Android it's the contacts app's own picker (ACTION_PICK). One picker opens at a time: a second pick() while one is open rejects with busy.

Read the address book

With permissions.contacts set, getAll() reads the address book:

async findFriends() {
  const contacts = await this.#contacts.getAll({ fields: ['phones'] });
  const numbers = contacts.flatMap((contact) => contact.phones);
  this.friends.set(await this.#api.whoUses(numbers));
}

getAll() asks the person first if they haven't been asked. It resolves with the contacts sorted by name, as the phone sorts them.

fields says which lists to read besides the id and the name: phones, emails or postalAddresses, with phones and emails by default. The others come back empty, so reading fewer is faster and keeps less of the person's data in the app.

Tell the person first

App Store Review (guidelines 5.1.1 and 5.1.2) and Google Play's User Data policy ask an app that uploads contacts to say so, and why, before it does. Show a screen that says what the app sends and what for, and call requestPermission() or getAll() from its button.

Limited access

From iOS 18, the person can share only some of their contacts. permission() is then limited, and getAll() reads only the contacts they shared. Android has no limited access.

LynxContacts

pick()

pick(options?: { select?: 'contact' | 'phone' | 'email' }): Promise<Contact | null>;

Opens the system's contact picker and resolves with what the person picks (see the table above), or null when they close it. Rejects with a ContactsError: unavailable where there's no picker (Lynx Explorer, or Android without a contacts app), busy while one is open. Throws a TypeError for another select.

getAll()

getAll(options?: { fields?: readonly ('phones' | 'emails' | 'postalAddresses')[] }): Promise<readonly Contact[]>;

Reads every contact the app may, asking first if the person hasn't been asked. Rejects with a ContactsError: permission-denied when the person refuses, unavailable without permissions.contacts (and in Lynx Explorer and the web preview), busy while Android shows another permission prompt. Throws a TypeError for another field.

permission

readonly permission: Signal<'granted' | 'limited' | 'denied' | 'undetermined' | 'unsupported'>;

Whether the app may read the address book. Reading it never asks. It's read when the service starts and each time the app comes back to the foreground, since the person can change it in Settings meanwhile.

ValueMeans
grantedThe person allowed the whole address book
limitedThe person shared some contacts (iOS 18 and later): getAll() reads those
deniedThe person refused. iOS doesn't ask again, and Android (from Android 11) stops asking after a second refusal: only Settings changes it, and openAppSettings() opens the app's page there
undeterminedNot asked yet: the next getAll() or requestPermission() asks. Android can't tell an app whether it asked before, so an app starts here
unsupportedThe app can't read the address book: no permissions.contacts, Lynx Explorer or the web preview

requestPermission()

requestPermission(): Promise<ContactsPermission>;

Asks the person, if they haven't been asked, and resolves with permission after they answer. getAll() asks by itself: call this to ask at a moment of the app's choosing. Rejects with busy while Android shows another permission prompt.

Contact

type Contact = {
  readonly id: string;
  readonly name: string;
  readonly phones: readonly string[];
  readonly emails: readonly string[];
  readonly postalAddresses: readonly string[];
};
  • id: the same from pick() and getAll(): Contacts' identifier on iOS and the contact's lookup key on Android, which survive the person editing the contact. Empty in the web preview.
  • name: as the phone shows it, or the company's name for a company.
  • phones, emails: as the person typed them.
  • postalAddresses: each formatted as the phone shows it, its lines separated by \n.

ContactsError

code is unavailable, permission-denied, busy or failed (anything else, with the platform's reason in message).

The web preview

pick() uses the browser's Contact Picker API, which only Chrome on Android has, and rejects with unavailable elsewhere. The browser gives no id, and lets the person pick a contact rather than one of its numbers, so 'phone' and 'email' keep its first. getAll() is unavailable.

Testing

The testing library fakes both: answerContactPicker() closes the picker with a contact, addContact() fills the address book and setContactsPermission() sets what the person answered.

fireEvent.tap(screen.getByText('Invite a friend'));
answerContactPicker({ name: 'Ada', phones: ['+1 555 0100'] });
await waitForUpdate();

expect(screen.getByText('Inviting Ada at +1 555 0100')).toBeInTheDocument();