react-native-contacts

repository·master·Indexed 23 days ago

https://github.com/morenoh149/react-native-contacts

A 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.

Tokens
5.3K
Snippets
15
Records
33
Agent score
76%

What's inside react-native-contacts

  1. Configure Android permissions

    master

    Android 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" />
  2. Configure iOS permissions

    master

    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.

  3. Request Android permissions at runtime

    master

    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);
            });
  4. Add numbers to an existing contact with editExistingContact

    master

    Use Contacts.editExistingContact(newPerson) to add one or more phone numbers to an existing contact.

    • Android behavior: The system's contact editing page will be opened.
    • iOS behavior: The existing contact will be opened, allowing for further manual modification.

    Known Android Bugs:

    • Custom labels may be overwritten to "Other".
    • Postal address update code is currently missing (though it exists for addContact).
    var newPerson = {
      recordID: '6b2237ee0df85980',
      phoneNumbers: [{
        label: 'mobile',
        number: '(555) 555-5555',
      }, ...
      ]
    }
    
    Contacts.editExistingContact(newPerson).then(contact => {
        //contact updated
    });
  5. Open the native contact creation form

    master

    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
    })
  6. Manage contact permissions on iOS

    master

    The library provides checkPermission and requestPermission methods.

    Important Notes:

    • Platform Support: These methods are only useful on iOS. For Android, use the standard React Native Permissions documentation.
    • Behavior: These methods do not re-request permission if it has already been granted or denied. On iOS, if permission is denied, you must instruct the user to enable contacts manually via Settings > [app name] > contacts.

    Permission Status Values:

    • Contacts.PERMISSION_AUTHORIZED
    • Contacts.PERMISSION_UNDEFINED
    • Contacts.PERMISSION_LIMITED
    • Contacts.PERMISSION_DENIED
    Contacts.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
      }
    })