1

I am trying to add a new contact to the Address book using react-native-contacts plugin, however I am getting the following error

java.lang.SecurityException: Permission Denial: writing com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/raw_contacts from pid=30018, uid=10148 requires android.permission.WRITE_CONTACTS, or grantUriPermission()

added these permissions on AndroidMainfest.xml

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

import Contacts from 'react-native-contacts'

let newPerson = { recordID: '507', rawContactId: '504', givenName: values.givenName, familyName: values.familyName, phoneNumbers: [ { label: values.type, number: values.phoneNumber, }, ], emailAddresses: [ { label: 'my email label', email: 'test@test.com', }, ], } Contacts.addContact(newPerson, (err, contact) => { if (err) throw err console.log(contact) // save successful })

It should add contact to the address book

prashant
  • 31
  • 2
  • what version of react native and android are your running? please use the latest versions of android, react-native and react-native-contacts – Harry Moreno Apr 01 '20 at 05:19

1 Answers1

0

You need to ask your phone for writing permission on your app load (or any other time u wanna add new contacts).

PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
  {
    'title': 'Contacts',
    'message': 'This app would like to view your contacts.',
    'buttonPositive': 'Please accept bare mortal'
  }
)

ask permission from your phone like mentioned above.