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 andpermissions.contactsin 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:
What it adds:
- iOS:
NSContactsUsageDescription, the reason iOS's prompt shows. - Android: the
READ_CONTACTSpermission, 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
pick() resolves with a Contact, or null when the person closes the picker. What the person picks is set by select:
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:
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()
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()
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
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.
requestPermission()
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
id: the same frompick()andgetAll(): Contacts'identifieron 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.
