S.M_Emamian
S.M_Emamian

Reputation: 17383

How to read contacts - react native

I want to read all contacts. I'm using this library:

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

I installed it and I added this permission on manifest.xml :

<uses-permission android:name="android.permission.READ_CONTACTS" />

but when I want to call getAll function my app is show me Unfortunately, *** has stopped

 componentDidMount(){
   Contacts.getAll((err, contacts) => {
  if (err) throw  err

    //console.log(contacts)
  })
 }

Upvotes: 1

Views: 9865

Answers (4)

Navneet kumar
Navneet kumar

Reputation: 1964

New versions of android need explicit user permissions apart from the manifest.xml

PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
      title: "Contacts",
      message: "This app would like to view your contacts."
    }).then(() => {
      Contacts.getAll((err, contacts) => {
        if (err === "denied") {
          // error
        } else {
          console.log(contacts[0]);
        }
      });
    });

Upvotes: 4

Carlos Emilio
Carlos Emilio

Reputation: 1

you need request Premissions for users.

https://facebook.github.io/react-native/docs/permissionsandroid.html

async requestReadContactsPermission() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
        {
          'title': 'App Premission',
          'message': 'Chat x App need permission.'
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("You can read contacts")
      } else {
        console.log("read contacts permission denied")
      }
    } catch (err) {
      console.warn(err)
    }
  }

Then you need to check if the permissions are authorized

listContacts() {
    this.requestReadContactsPermission().then(
        Contacts.checkPermission((err, permission) => {
            if (err) throw err;

            // Contacts.PERMISSION_AUTHORIZED || Contacts.PERMISSION_UNDEFINED || Contacts.PERMISSION_DENIED
            if (permission === 'undefined') {
              Contacts.requestPermission((err, permission) => {
                // ...
              })
            }
            if (permission === 'authorized') {
              this.getAllContacts()
            }
            if (permission === 'denied') {
              // x.x
            }
          })
    )

}

After this you can read the contact list

    Contacts.getAll((err, contacts) => {
      if (err) throw err;

          // contacts returned
         console.log(contacts)
      })

Upvotes: 0

Jamal Hassouni
Jamal Hassouni

Reputation: 79

Install package with npm

npm install react-native-contacts

Install package with yarn

yarn add react-native-contacts

and then do

react-native link react-native-contacts

Add the read contact permission to your AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS" />

simple code

import Contacts from 'react-native-contacts';
componentDidMount(){
 Contacts.getAll((err, contacts) => {
  if (err) throw err;
  // contacts returned
  console.log(contacts)
})
}

then

1 - You must rebuild your application

2 - Then go to the application settings and give permission to the contacts

3 - react-native run-android

Upvotes: 0

Coder Absolute
Coder Absolute

Reputation: 6347

Install the library, am using npm for this example:

npm install react-native-contacts

Add the read contact permission to your AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS" />

This is the basic code example.

import Contacts from 'react-native-contacts';

Contacts.getAll((err, contacts) => {
  if (err) throw err;

  // contacts returned
  console.log(contacts)
})

Ensure that you are actually doing the import.

Upvotes: 0

Related Questions