Anu
Anu

Reputation: 773

Unable to fetch contacts in my react-native application from phone's contact book

I have tried

yarn add react-native-contacts

plugin and followed all steps mentioned in this link

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

but its giving error

undefined is not an object(evaluating Contacts.getAll).

Is there anyother way to fetch contacts from phonebook?Kindly let me know.

Thanks

Upvotes: 3

Views: 2394

Answers (2)

JSnow
JSnow

Reputation: 1029

There is an npm package called "react-native-contacts-wrapper" which does the job wonderfully.

You can add it in your application like this-

import ContactsWrapper from 'react-native-contacts-wrapper';

onContactSelect(){
    ContactsWrapper.getContact()
    .then((contact) => {
        this.setState({
            importingContactInfo:true,
            guest:contact.name,
            email:contact.email,
            phone:contact.phone
        });
    })
    .catch((error) => {
        console.log("ERROR CODE: ", error.code);
        console.log("ERROR MESSAGE: ", error.message);
    });
}

Upvotes: 0

Anu
Anu

Reputation: 773

STEP-1. yarn add react-native-contacts --save

STEP-2. react-native link react-native-contacts

STEP_3. Add following code manually in the files mentioned:

AndroidManifest.xml

Add following permissions:-

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

MainApplication.java

import com.rt2zz.reactnativecontacts.ReactNativeContacts; //import this package

protected List<ReactPackage> getPackages() {
      mCallbackManager = new CallbackManager.Factory().create();
      return Arrays.<ReactPackage>asList(
      new ReactNativeContacts()  // Add this
   );
 }

android/settings.gradle

include ':react-native-contacts'
project(':react-native-contacts').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-contacts/android')

android/app/build.gradle Within dependencies block add this:-

 compile project(':react-native-contacts')

Note:- Installation can also be done using npm if you are not using yarn Eg:-

  1. npm install react-native-contacts
  2. react-native link react-native-contacts

Upvotes: 4

Related Questions