requestPermission.js 799 B

123456789101112131415161718192021222324252627282930
  1. import { PermissionsAndroid } from 'react-native';
  2. const hasPermission = result => {
  3. return result === PermissionsAndroid.RESULTS.GRANTED;
  4. };
  5. const checkContactsPermission = async () => {
  6. return new Promise(async (resolve, reject) => {
  7. try {
  8. const check = await PermissionsAndroid.check(
  9. PermissionsAndroid.PERMISSIONS.READ_CONTACTS
  10. );
  11. if (!check) {
  12. const result = await PermissionsAndroid.request(
  13. PermissionsAndroid.PERMISSIONS.READ_CONTACTS
  14. );
  15. if (result === PermissionsAndroid.RESULTS.GRANTED) {
  16. resolve(hasPermission(result));
  17. }
  18. } else {
  19. resolve(true);
  20. }
  21. } catch (err) {
  22. console.warn(err);
  23. reject('Error', false);
  24. }
  25. });
  26. };
  27. export default checkContactsPermission;