Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get contacts from phone using react native expo

Get contacts from phone using react native expo I'm not getting it

like image 553
Syed Abdul Rehman Avatar asked Mar 07 '23 17:03

Syed Abdul Rehman


1 Answers

Use Contacts and Permissions, get user permission and then take the data as stated in the Expo docs. Here is an example: Snack Contact Example

async showFirstContactAsync() {
  // Ask for permission to query contacts.
  const permission = await Permissions.askAsync(Permissions.CONTACTS);

  if (permission.status !== 'granted') {
    // Permission was denied...
    return;
  }
  const contacts = await Contacts.getContactsAsync({
    fields: [
      Contacts.PHONE_NUMBERS,
      Contacts.EMAILS,
    ],
    pageSize: 10,
    pageOffset: 0,
  });
  if (contacts.total > 0) {
    Alert.alert(
      'Your first contact is...',
      `Name: ${contacts.data[0].name}\n` +
      `Phone numbers: ${contacts.data[0].phoneNumbers[0].number}\n` +
      `Emails: ${contacts.data[0].emails[0].email}`
    );
  }
}
like image 193
Bostrot Avatar answered Mar 23 '23 22:03

Bostrot