morenoh149 / react-native-contacts

React Native Contacts
MIT License
1.64k stars 560 forks source link

IOS cannot read property Contacts.getAll of undefined #691

Closed YosefOberlander closed 2 years ago

YosefOberlander commented 2 years ago

React native version - 0.69.2 React version - 18.0.0 Bare react native project.

Installed via yarn and ran pod install successfully. The app builds fine but when calling any Contacts function, it returns an error Ex: cannot read cannot read property Contacts.getAll of undefined. On android everything works fine.

Here's the code. useEffect(() => { loadContacts() }, [])

const loadContacts = () => { Contacts.getAllWithoutPhotos() .then(contacts => { console.log('contacts', contacts) setContacts(contacts) }) .catch(e => { Toast.show({ type: 'error', text1: 'There was a problem trying to get you phone contacts.' }) }); }

YosefOberlander commented 2 years ago

Solved. Added IOS permissions contacts.checkPermission()

zfine416 commented 6 months ago

@YosefOberlander Where did you add those permissions?

YosefOberlander commented 6 months ago

@zfine416 Here's the code. I called checkContactPermissions() in a useEffect. Hope it helps.

    const checkContactPermissions = () => {
        if (Platform.OS === "android") {
            PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
                title: "Contacts",
                message: "This app would like to view your contacts."
            })
                .then(() => {
                    setIsLoading(false);
                    setHasPermission(true);
                    loadContacts();
            });
        } else {
            Contacts.checkPermission().then(permission => {
                // Contacts.PERMISSION_AUTHORIZED || Contacts.PERMISSION_UNDEFINED || Contacts.PERMISSION_DENIED
                if (permission === 'undefined') {
                    Contacts.requestPermission().then(permission => {
                        if (permission === 'authorized') {
                            setHasPermission(true);
                            loadContacts();
                        }

                        if (permission === 'denied') {
                            setIsLoading(false);
                            return Toast.show({
                                type: 'error',
                                text1: 'Contacts Permissions Are Off',
                                text2: 'Please turn on contact permissions to see contacts.'
                            })
                        }

                        if (permission === 'undefined') {
                            checkContactPermissions();
                        }

                        setIsLoading(false);
                    })
                }
                if (permission === 'authorized') {
                    setHasPermission(true);
                    loadContacts()
                }
                if (permission === 'denied') {
                    setIsLoading(false);
                    return Toast.show({
                        type: 'error',
                        text1: 'Contacts Permissions Are Off',
                        text2: 'Please turn on contact permissions to see contacts.'
                    })
                }

                setIsLoading(false);
            })
        }
    }