> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# 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`](#turn-it-on) 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](/guide/native-apps/permissions.md) iOS shows when it asks:

```json title="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](/guide/native-apps/device-features.md) for every setting.

## Pick a contact

```typescript
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`:

| `select`              | The person picks           | The contact comes with                                                                                                                           |
| --------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `'phone'`             | one of a contact's numbers | its id, its name and that number in `phones`, on every platform                                                                                  |
| `'email'`             | one email address          | its id, its name and that address in `emails`, on every platform                                                                                 |
| `'contact'` (default) | a whole contact            | **iOS:** 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`](#turn-it-on) set, `getAll()` reads the address book:

```typescript
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()`

```typescript
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()`

```typescript
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`

```typescript
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.

| Value          | Means                                                                                                                                                                                                                                                         |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `granted`      | The person allowed the whole address book                                                                                                                                                                                                                     |
| `limited`      | The person shared some contacts (iOS 18 and later): `getAll()` reads those                                                                                                                                                                                    |
| `denied`       | The person refused. iOS doesn't ask again, and Android (from Android 11) stops asking after a second refusal: only Settings changes it, and [`openAppSettings()`](/api/angular-lynx/sharing/linking.md#lynxlinkingopenappsettings) opens the app's page there |
| `undetermined` | Not asked yet: the next `getAll()` or `requestPermission()` asks. Android can't tell an app whether it asked before, so an app starts here                                                                                                                    |
| `unsupported`  | The app can't read the address book: no `permissions.contacts`, Lynx Explorer or the web preview                                                                                                                                                              |

### `requestPermission()`

```typescript
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`

```typescript
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](https://developer.mozilla.org/en-US/docs/Web/API/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()`](/api/testing-library/fakes/people.md#answercontactpicker) closes the picker with a contact, `addContact()` fills the address book and `setContactsPermission()` sets what the person answered.

```typescript
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();
```
