Search code examples
javascriptiosreact-nativepermissionsexpo

Expo Contact: Request Permission to access contact if it is status is denied


I have a contact component that works as expected, but if a user initially denies the request to grant my app access to their contact the request is not being asked again, the contact modal instead appears empty:

  useEffect(() => {
    (async () => {
      const { status } = await Contacts.requestPermissionsAsync()

      if (status === "granted") {
        const { data } = await Contacts.getContactsAsync({
          fields: [Contacts?.Fields?.PhoneNumbers],
        });
        if (data.length > 0) {
          setContacts(data);
        }
      }
    })();
  }, []);

Solution

  • Check canAskAgain if the permission can be requested, if not give a user option to open Settings.

    Indicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission.

    const { status, canAskAgain } = await Contacts.requestPermissionsAsync()
    ...
    if (canAskAgain) {
      Linking.openSettings()
    }