Closed yanivbs closed 2 years ago
@yanivbs Did you find the solution ?
The truth is I got stuck with it, I noticed that the error occurs when I try to access the contacts in this way:
openForm = () => {
Contacts.openContactForm({}, (err) => {
if (err) {
console.warn(err)
}
})
}
It works for me when I change the code to something like this:
getContacts = () => {
this.requestContactsPermission().then((didGetPermission) => {
if (didGetPermission) {
Contacts.getAll()
.then((contacts) => {
this.setState({
myContacts: contacts
})
alert("rich to here")
//console.warn(Contacts)
}).catch((e) => { alert('no permission') })
}
})
}
The thing is that I do not always manage to use this method, for example in this section the two methods do not work for me and with this code:
openForm = () => {
Contacts.openContactForm({}, (err) => {
if (err) {
console.warn(err)
}
})
}
I get this error:
And in this code:
openForm = () => {
Contacts.openContactForm({}, (err) => {
}).then(console.warn("gff")).catch((e) => { console.warn(err) });
}
I get this error :
// App.js
import React, { useState, Component } from 'react';
import { Appbar } from 'react-native-paper';
import { View, StyleSheet, Header, StatusBar, Image, Text, SafeAreaView, ImageBackground, Button, Platform, PermissionsAndroid, requestMultiple, Linking, ErrorHandlerCallback, ErrorUtils } from 'react-native';
import Contacts from 'react-native-contacts';
class App extends Component {
state = {
myContacts: []
}
async requestContactsPermission() {
if (Platform.OS === 'ios') {
return true
} else {
const granted = await PermissionsAndroid.requestMultiple([PermissionsAndroid.PERMISSIONS.READ_CONTACTS, PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS]);
if (granted['android.permission.READ_CONTACTS'] === PermissionsAndroid.RESULTS.GRANTED && granted['android.permission.WRITE_CONTACTS'] === PermissionsAndroid.RESULTS.GRANTED) {
return true
} else {
return false
}
}
}
// Getting my contacts ->
getContacts = () => {
this.requestContactsPermission().then((didGetPermission) => {
if (didGetPermission) {
Contacts.getAll()
.then((contacts) => {
this.setState({
myContacts: contacts
})
alert("rich to here")
//console.warn(Contacts)
}).catch((e) => { alert('no permission') })
}
})
}
// Getting my contacts //
// Adding new contact ->
addContacts = () => {
this.requestContactsPermission().then((didGetPermission) => {
if (didGetPermission) {
const newContact = {
emailAddresses: [{
label: "work",
email: "yaniv.welding@gmail.com",
}],
familyName: "Ben-Shetrit",
givenName: "Yaniv",
phoneNumbers: [{
label: "mobile",
number: "(053) 7276021",
}],
}
Contacts.addContacts(newContact)
.then((contacts) => {
this.getContacts();
}).catch((e) => { alert('no permission') })
}
})
}
// Adding new contact //
// opening new form ->
openForm = () => {
Contacts.openContactForm({}, (err) => {
if (err) {
console.warn(err)
}
})
}
// opening new form
render() {
return (
<View style={styles.container}>
{this.state.myContacts.map((item, i) => (
<Text key={i}>{item.givenName} {item.familyName}</Text>))
}
<Button title="Load contacts" onPress={this.getContacts}/>
<Button title="Add contacts" onPress={this.addContacts} />
<Button title="open form" onPress={this.openForm} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin:10
}
})
export default App;
This issue is stale, please provide more information about the status
I am trying to view the contacts in Android Emulator,
I manage to get the permission, but for some reason the compiler is unable to access contacts and displays this error :
I have implemented all the installation requirements for working with contacts.
// App.js
// build.gradle dependencies
dependencies { implementation project(':react-native-contacts')
}`
//MainApplication.java
// AndroidManifest.xml