0

I want to make a screen page that can be navigated in the contact list on my phone. so I made a native module for get contact, It worked but for android 10 and below. For android 11 he can't run. The function I created can't get contact data.I've made sure all the permissions are there. Below is the code I made

const getChooseContact = () => {
    const { ContactsWrapper } = NativeModules;

    ContactsWrapper.getContact()
      .then((contact) => {
        setPhoneNumber(contact.phoneNumber);
      })
      .catch((error) => {
        console.log(error);
      });
  };

const requestMediaPermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
      );

      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        getChooseContact();
      } else {
        setErrorMessage('Access media permission denied');
      }
    } catch (err) {
      console.log(err);
    }
  };
zainal abidin
  • 363
  • 1
  • 3
  • 12

1 Answers1

0

This is majorly because in API 30 and above we can no-longer interact with external modules without specifically being allowed the interaction. This can happen in two ways one using the <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" /> inside the androidmanifest.xml though this is discouraged since it can have your app rejected Read this kindly https://support.google.com/googleplay/android-developer/answer/10158779#zippy=%2Cpermitted-uses-of-the-query-all-packages-permission

to fix your issue

  1. inside build.gradle under android update class paths to this : classpath 'com.android.tools.build:gradle:3.5.4' this allows for the next step to be recognised during bundling otherwise your will get an error
  2. inside androidmanifest.xml before the application tag add this
 <queries>
        <!-- Browser -->
        
        <intent android:label="View Contact">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.cursor.item/person" android:host="contacts" />
         
        </intent>


    </queries>