This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 联系人(旧版) iconExpo 联系人(旧版)

提供对手机系统联系人的访问的库。

Android
iOS
Included in Expo Go
Recommended version:
~58.0.6

expo-contacts 提供对设备系统联系人访问的功能,允许你获取联系人信息,以及添加、编辑或删除联系人。

在 iOS 上,联系人有一个多层分组系统,你也可以通过此 API 访问该系统。

🌐 On iOS, contacts have a multi-layered grouping system that you can also access through this API.

安装

🌐 Installation

Terminal
- npx expo install expo-contacts
- yarn expo install expo-contacts
- pnpm expo install expo-contacts
- bun expo install expo-contacts

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),可以使用其内置的 配置插件 来配置 expo-contacts。该插件允许你配置无法在运行时设置并且需要构建新的应用二进制文件才能生效的各种属性。如果你的应用不使用 CNG,则需要手动配置该库。

🌐 You can configure expo-contacts using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-contacts", { "contactsPermission": "Allow $(PRODUCT_NAME) to access your contacts." } ] ] } }

Configurable properties

NameDefaultDescription
contactsPermission"Allow $(PRODUCT_NAME) to access your contacts"
Only for: 
iOS

A string to set the NSContactsUsageDescription permission message.

Are you using this library in an existing React Native app?

如果你没有使用连续原生生成(CNG)(而是手动使用原生 Android 和 iOS 项目),那么你需要在原生项目中配置以下权限:

🌐 If you're not using Continuous Native Generation (CNG) (you're using native android and ios projects manually), then you need to configure following permissions in your native projects:

  • 对于 Android,在项目的 android/app/src/main/AndroidManifest.xml 中添加 android.permission.READ_CONTACTS 和 android.permission.WRITE_CONTACTS 权限:

    <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
  • 对于 iOS,在你的项目 ios/[app]/Info.plist 中添加 NSContactsUsageDescription 键:

    <key>NSContactsUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your contacts</string>

用法

🌐 Usage

Basic Contacts usage
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>Contacts Module Example</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });

应用接口

🌐 API

import * as Contacts from 'expo-contacts/legacy';

No API data file found, sorry!

权限

🌐 Permissions

安卓

🌐 Android

此库会自动为你的应用添加 READ_CONTACTS 和 WRITE_CONTACTS 权限:

🌐 This library automatically adds READ_CONTACTS and WRITE_CONTACTS permissions to your app:

Android permissionDescription

READ_CONTACTS

Allows an application to read the user's contacts data.

WRITE_CONTACTS

Allows an application to write the user's contacts data.

iOS

该库使用以下用法描述键:

🌐 The following usage description keys are used by this library:

Info.plist keyDescription

NSContactsUsageDescription

A message that tells the user why the app is requesting access to the user’s contacts.