Run the example project on Android
masterEnsure no other packagers are running and an Android emulator is active (via Android Studio AVD Manager) before running the application.
npx react-native run-androidrepository·master·Indexed 23 days ago
https://github.com/morenoh149/react-native-contactsA React Native library (version 8.0.10) that provides a bridge to access, create, edit, and delete contacts on iOS and Android devices. It includes core API methods such as getAll(), addContact(), updateContact(), and deleteContact(), as well as platform-specific functionality for iOS group management and Android photo path writing.
Ensure no other packagers are running and an Android emulator is active (via Android Studio AVD Manager) before running the application.
npx react-native run-androidTo set up the example project locally, clone the repository, navigate to the example directory, and install dependencies using yarn.
git clone https://github.com/rt2zz/react-native-contacts.git
cd react-native-contacts/example
yarn installYou can install the library using npm or yarn.
npm:
npm install react-native-contacts --saveyarn:
yarn add react-native-contactsEnsure no other packagers are running. You can run the project either with or without CocoaPods depending on your environment setup.
### Without CocoaPods
```bash
npx react-native run-ioscd ios && pod install && cd ..
npx react-native run-iosAndroid requires explicit permission declarations in your AndroidManifest.xml.
For API 23+:
Add READ_CONTACTS to view contacts. If your app creates contacts, also add WRITE_CONTACTS.
For API 22 and below:
Add READ_PROFILE and/or WRITE_PROFILE.
Example for API 23+:
<uses-permission android:name="android.permission.WRITE_CONTACTS" />To prevent your app from crashing when requesting contact permissions on iOS, you must add the Privacy - Contacts Usage Description key to your Info.plist file.
While the value for this key is optional during development, it must provide a clear explanation of why your app needs access to contacts when submitting to the App Store.
node_modules, then reinstalling dependencies and rebuilding.On Android, you must request permissions using PermissionsAndroid before calling contact methods.
import { PermissionsAndroid } from 'react-native';
import Contacts from 'react-native-contacts';
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
title: 'Contacts',
message: 'This app would like to view your contacts.',
buttonPositive: 'Please accept bare mortal',
})
.then((res) => {
console.log('Permission: ', res);
Contacts.getAll()
.then((contacts) => {
// work with contacts
console.log(contacts);
})
.catch((e) => {
console.log(e);
});
})
.catch((error) => {
console.error('Permission error: ', error);
});Use Contacts.editExistingContact(newPerson) to add one or more phone numbers to an existing contact.
Known Android Bugs:
addContact).var newPerson = {
recordID: '6b2237ee0df85980',
phoneNumbers: [{
label: 'mobile',
number: '(555) 555-5555',
}, ...
]
}
Contacts.editExistingContact(newPerson).then(contact => {
//contact updated
});Use openContactForm(contact) to display the device's default contact creation UI. This allows users to edit the contact before saving it to the phone book. The method returns a Promise that resolves with the saved contact once the user clicks save or cancel.
var newPerson = {
emailAddresses: [{
label: "work",
email: "mrniet@example.com",
}],
familyName: "Nietzsche",
givenName: "Friedrich",
displayName: "Friedrich Nietzsche"
}
Contacts.openContactForm(newPerson).then(contact => {
// contact has been saved
})The thumbnailPath property on a contact object provides a direct URI to the temporary location of the contact's cropped thumbnail image. You can use this URI directly in a React Native <Image /> component.
<Image source={{uri: contact.thumbnailPath}} />The library provides checkPermission and requestPermission methods.
Important Notes:
Settings > [app name] > contacts.Permission Status Values:
Contacts.PERMISSION_AUTHORIZEDContacts.PERMISSION_UNDEFINEDContacts.PERMISSION_LIMITEDContacts.PERMISSION_DENIEDContacts.checkPermission().then(permission => {
// permission can be: 'authorized', 'undefined', 'limited', or 'denied'
if (permission === 'undefined') {
Contacts.requestPermission().then(permission => {
// ...
})
}
if (permission === 'authorized') {
// yay!
}
if (permission === 'limited') {
// ...
}
if (permission === 'denied') {
// x.x
}
})