This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 联系人(旧版)
一个提供访问手机系统联系人的库。
重要
expo-contacts库中包含Contacts API的legacy版本。它可以与从根目录导出的基于类的expo-contactsAPI 一起使用。要使用旧版 API,请从expo-contacts/legacy导入。
expo-contacts 提供对设备系统联系人的访问,允许你获取联系人信息,以及添加、编辑或删除联系人。
在 iOS 上,联系人具有多层级分组系统,你也可以通过此 API 访问。
安装
- npx expo install expo-contactsIf you are installing this in an existing React Native app, make sure to install expo in your project.
应用配置中的配置
如果你在项目中使用配置插件(持续原生生成(CNG)),可以使用 expo-contacts 内置的 配置插件 来进行配置。该插件允许你配置一些无法在运行时设置的属性,这些属性需要重新构建新的应用二进制文件后才会生效。如果你的应用不使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-contacts", { "contactsPermission": "允许 $(PRODUCT_NAME) 访问你的联系人。" } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
contactsPermission | "允许 $(PRODUCT_NAME) 访问你的联系人" | Only for: iOS 设置 |
Are you using this library in an existing React Native app?
如果你没有使用持续原生生成(CNG)(即你在手动使用原生 android 和 ios 项目),那么你需要在原生项目中配置以下权限:
-
对于 Android,请将
android.permission.READ_CONTACTS和android.permission.WRITE_CONTACTS权限添加到项目的 android/app/src/main/AndroidManifest.xml 中:<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> -
对于 iOS,请将
NSContactsUsageDescription键添加到项目的 ios/[app]/Info.plist 中:<key>NSContactsUsageDescription</key> <string>允许 $(PRODUCT_NAME) 访问你的联系人</string>
用法
import { useEffect } from 'react'; import { StyleSheet, View, Text } from 'react-native'; import * as Contacts from 'expo-contacts/legacy'; export default function App() { useEffect(() => { (async () => { const { status } = await Contacts.requestPermissionsAsync(); if (status === 'granted') { const { data } = await Contacts.getContactsAsync({ fields: [Contacts.Fields.Emails], }); if (data.length > 0) { const contact = data[0]; console.log(contact); } } })(); }, []); return ( <View style={styles.container}> <Text>联系人模块示例</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });
API
import * as Contacts from 'expo-contacts/legacy';
Component
Type: React.PureComponent<ContactAccessButtonProps>
Creates a contact access button to quickly add contacts under limited-access authorization.
For more details, you can read the Apple docs about the underlying ContactAccessButton SwiftUI view.
ColorValueA color of the button's background. Provided color should not be transparent, otherwise it may not satisfy platform requirements for button legibility.
stringWhen the query produces a single result, the contact access button shows the caption under the matching contact name. It can be nothing (default), email address or phone number.
Acceptable values are: 'default' | 'email' | 'phone'
string[]An array of email addresses. The search omits contacts matching query that also match any email address in this array.
string[]An array of phone numbers. The search omits contacts matching query that also match any phone number in this set.
stringA string to match against contacts not yet exposed to the app. You typically get this value from a search UI that your app presents, like a text field.
ColorValueA color of the button's title. Slightly dimmed version of this color is used for the caption text. Make sure there is a good contrast between the text and the background, otherwise platform requirements for button legibility may not be satisfied.
ColorValueA tint color of the button and the modal that is presented when there is more than one match.
Static methods
Returns a boolean whether the ContactAccessButton is available on the platform.
This is true only on iOS 18.0 and newer.
booleanConstants
Methods
Promise<string>| Parameter | Type |
|---|---|
| groupQuery | GroupQuery |
Promise<ContactsPermissionResponse>Promise<boolean>Promise<boolean>Promise<string[]>Promise<ExistingContact | null>| Parameter | Type |
|---|---|
| contactId(optional) | string | null |
| contact(optional) | Contact | null |
| formOptions(optional) | FormOptions |
Promise<any>Promise<ContactsPermissionResponse>Event subscriptions
| Parameter | Type |
|---|---|
| listener | () => void |
EventSubscriptionTypes
| Property | Type | Description |
|---|---|---|
| city(optional) | string | City name. |
| country(optional) | string | Country name |
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| isoCountryCode(optional) | string | |
| label | string | Localized display name. |
| neighborhood(optional) | string | Neighborhood name. |
| poBox(optional) | string | P.O. Box. |
| postalCode(optional) | string | Local post code. |
| region(optional) | string | Region or state name. |
| street(optional) | string | Street name. |
Literal type: union
Acceptable values are: CalendarFormats | {CalendarFormats}
| Property | Type | Description |
|---|---|---|
| addresses(optional) | Address[] | Locations. |
| birthday(optional) | Date | Birthday information in Gregorian format. |
| company(optional) | string | Organization the entity belongs to. |
| contactType | ContactType | Denoting a person or company. |
| dates(optional) | Date[] | A labeled list of other relevant user dates in Gregorian format. |
| department(optional) | string | Job department. |
| emails(optional) | Email[] | Email addresses. |
| firstName(optional) | string | Given name. |
| image(optional) | Image | Thumbnail image. On iOS it size is set to 320×320px, on Android it may vary. |
| imageAvailable(optional) | boolean | Used for efficient retrieval of images. |
| instantMessageAddresses(optional) | InstantMessageAddress[] | Instant messaging connections. |
| isFavorite(optional) | boolean | Only for: Android Whether the contact is starred. |
| jobTitle(optional) | string | Job description. |
| lastName(optional) | string | Last name. |
| maidenName(optional) | string | Maiden name. |
| middleName(optional) | string | Middle name |
| name | string | Full name with proper format. |
| namePrefix(optional) | string | Dr., Mr., Mrs., and so on. |
| nameSuffix(optional) | string | Jr., Sr., and so on. |
| nickname(optional) | string | An alias to the proper name. |
| nonGregorianBirthday(optional) | Date | Only for: iOS Birthday that doesn't conform to the Gregorian calendar format, interpreted based on the calendar |
| note(optional) | string | Additional information.
|
| phoneNumbers(optional) | PhoneNumber[] | Phone numbers. |
| phoneticFirstName(optional) | string | Pronunciation of the first name. |
| phoneticLastName(optional) | string | Pronunciation of the last name. |
| phoneticMiddleName(optional) | string | Pronunciation of the middle name. |
| rawImage(optional) | Image | Raw image without cropping, usually large. |
| relationships(optional) | Relationship[] | Names of other relevant user connections. |
| socialProfiles(optional) | SocialProfile[] | Only for: iOS Social networks. |
| urlAddresses(optional) | UrlAddress[] | Associated web URLs. |
| Property | Type | Description |
|---|---|---|
| containerId(optional) | string | Only for: iOS Get all contacts that belong to the container matching this ID. |
| fields(optional) | FieldType[] | If specified, the defined fields will be returned. If skipped, all fields will be returned. |
| groupId(optional) | string | Only for: iOS Get all contacts that belong to the group matching this ID. |
| id(optional) | string | string[] | Get contacts with a matching ID or array of IDs. |
| name(optional) | string | Get all contacts whose name contains the provided string (not case-sensitive). |
| pageOffset(optional) | number | The number of contacts to skip before gathering contacts. |
| pageSize(optional) | number | The max number of contacts to return. If skipped or set to |
| rawContacts(optional) | boolean | Only for: iOS Prevent unification of contacts when gathering. Default: false |
| sort(optional) | ContactSort | Sort method used when gathering contacts. |
| Property | Type | Description |
|---|---|---|
| data | ExistingContact[] | An array of contacts that match a particular query. |
| hasNextPage | boolean | This will be |
| hasPreviousPage | boolean | This will be |
String union of SortTypes values.
Type: PermissionResponse extended by:
| Property | Type | Description |
|---|---|---|
| accessPrivileges(optional) | 'all' | 'limited' | 'none' | Indicates if your app has access to the whole or only part of the contact library. Possible values are:
|
| Property | Type | Description |
|---|---|---|
| contactId(optional) | string | Query all the containers that parent a contact. |
| containerId(optional) | string | string[] | Query all the containers that matches ID or an array od IDs. |
| groupId(optional) | string | Query all the containers that parent a group. |
| Property | Type | Description |
|---|---|---|
| day | number | Day. |
| format(optional) | CalendarFormatType | Format for the date. This is provided by the OS, do not set this manually. |
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| label(optional) | string | Localized display name. |
| month | number | Month - adjusted for JavaScript |
| year(optional) | number | Year. |
| Property | Type | Description |
|---|---|---|
| email(optional) | string | Email address. |
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| isPrimary(optional) | boolean | Flag signifying if it is a primary email address. |
| label | string | Localized display name. |
Type: Contact extended by:
| Property | Type | Description |
|---|---|---|
| id | string | Immutable identifier used for querying and indexing. This value will be generated by the OS when the contact is created. |
| Property | Type | Description |
|---|---|---|
| allowsActions(optional) | boolean | Actions like share, add, create. |
| allowsEditing(optional) | boolean | Allows for contact mutation. |
| alternateName(optional) | string | Used if contact doesn't have a name defined. |
| cancelButtonTitle(optional) | string | The name of the left bar button. Only applies when editing an existing contact. |
| displayedPropertyKeys(optional) | FieldType[] | The properties that will be displayed when viewing a contact. |
| groupId(optional) | string | The parent group for a new contact. |
| isNew(optional) | boolean | Present the new contact controller. If set to |
| message(optional) | string | The message displayed under the name of the contact. Only applies when editing an existing contact. |
| preventAnimation(optional) | boolean | Prevents the controller from animating in. |
| shouldShowLinkedContacts(optional) | boolean | Show or hide the similar contacts. |
| Property | Type | Description |
|---|---|---|
| id(optional) | string | The editable name of a group. |
| name(optional) | string | Immutable id representing the group. |
| Property | Type | Description |
|---|---|---|
| containerId(optional) | string | Query all groups that belong to a certain container. |
| groupId(optional) | string | Query the group with a matching ID. |
| groupName(optional) | string | Query all groups matching a name. |
| Property | Type | Description |
|---|---|---|
| base64(optional) | string | Image as Base64 string. |
| height(optional) | number | Only for: iOS Image height |
| uri(optional) | string | A local image URI.
|
| width(optional) | number | Only for: iOS Image width. |
| Property | Type | Description |
|---|---|---|
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| label | string | Localized display name. |
| localizedService(optional) | string | Localized name of app. |
| service(optional) | string | Name of instant messaging app. |
| username(optional) | string | Username in IM app. |
Literal type: union
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never' | number
An object obtained by permissions get and request functions.
| Property | Type | Description |
|---|---|---|
| canAskAgain | boolean | Indicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission. |
| expires | PermissionExpiration | Determines time when the permission expires. |
| granted | boolean | A convenience boolean that indicates if the permission is granted. |
| status | PermissionStatus | Determines the status of the permission. |
| Property | Type | Description |
|---|---|---|
| countryCode(optional) | string | Country code. Example
|
| digits(optional) | string | Phone number without format. Example
|
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| isPrimary(optional) | boolean | Flag signifying if it is a primary phone number. |
| label | string | Localized display name. |
| number(optional) | string | Phone number. |
| Property | Type | Description |
|---|---|---|
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| label | string | Localized display name. |
| name(optional) | string | Name of related contact. |
| Property | Type | Description |
|---|---|---|
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| label | string | Localized display name. |
| localizedProfile(optional) | string | Localized profile name. |
| service(optional) | string | Name of social app. |
| url(optional) | string | Web URL. |
| userId(optional) | string | Username ID in social app. |
| username(optional) | string | Username in social app. |
| Property | Type | Description |
|---|---|---|
| id(optional) | string | Unique ID. This value will be generated by the OS. |
| label | string | Localized display name. |
| url(optional) | string | Web URL. |
Enums
权限
Android
此库会自动为你的应用添加 READ_CONTACTS 和 WRITE_CONTACTS 权限:
| Android permission | Description |
|---|---|
Allows an application to read the user's contacts data. | |
Allows an application to write the user's contacts data. |
iOS
此库使用以下用途描述键: