skip to Main Content

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);
        }
      }
    })();
  }, []);

2

Answers


  1. Chosen as BEST ANSWER

    I had to install expo-linking using npx expo install expo-linking

     (async () => {
          const { status, canAskAgain } = await Contacts.requestPermissionsAsync();
    
          if (status === "granted") {
            const { data } = await Contacts.getContactsAsync({
              fields: [Contacts?.Fields?.PhoneNumbers],
            });
            if (data.length > 0) {
              setContacts(data);
            }
          } else {
            Linking.openSettings();
          console.log(canAskAgain)
          }
        })();
      }, []);
    

  2. 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()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search