streem / react-native-select-contact

A cross-platform contact selection library for react-native
MIT License
114 stars 50 forks source link

Android Permissions error #19

Open SCasarotto opened 4 years ago

SCasarotto commented 4 years ago

I am receiving an android permission error even though I add this <uses-permission android:name="android.permission.READ_CONTACTS" /> to my AndroidManifest. any guesses? Works just fine on iOS. Even tried the react-native link but that didn't seem to resolve it.

ropmansk commented 4 years ago

You need to request the READ_CONTACTS permission using PermissionsAndroid if your targetSdkVersion > 22.

GusNando commented 4 years ago

hi, import permision android import {PermissionsAndroid} from 'react-native';

and use this two method

getPhoneNumber = async () => {
    return selectContactPhone().then(selection => {
      if (!selection) {
        return null;
      }

      let {contact, selectedPhone} = selection;
      console.log(
        `Selected ${selectedPhone.type} phone number ${selectedPhone.number} from ${contact.name}`,
      );
      return selectedPhone.number;
    });
  }

requestContactPermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
        {
          title: 'Allow Access Contact?',
          message:
            'allow this app to read contact information',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('granted');
        this.getPhoneNumber();
      } else {
        console.log('denied');
      }
    } catch (err) {
      console.warn(err);
    }
  }

and call from jsx this.requestContactPermission();

have a nice day :D

moxspoy commented 4 years ago

thank you @GusNando for the explanation. in my Xiaomi Redmi 6, after popup permission show and click the Allow button, the code after await request permission is not excuted, so i can not get the granted value. How to fix it?

GusNando commented 4 years ago

did you added <uses-permission android:name="android.permission.READ_CONTACTS" /> in android manifest? and can you share some of your code?

ameer0706 commented 1 year ago

import {PermissionsAndroid} from 'react-native';