11.5 C
London
Tuesday, May 21, 2024

ios – Issues fetching contacts with expo-contacts


useEffect(() => {
    console.log("Checking present person...");
    if (auth.currentUser) {
      const uid = auth.currentUser.uid;
      fetchUserData(uid)
        .then((information) => {
          console.log("Person information fetched:", information);
          setUser(information as Person);
          fetchContacts();
        })
        .catch((error) => console.log("Error fetching person information:", error));
    }
  }, [auth.currentUser]);

  const fetchContacts = async () => {
    attempt {
      console.log("Requesting contacts permission...");
      const { standing } = await Contacts.requestPermissionsAsync();
      console.log("Contacts permission standing:", standing);
      setPermissionStatus(standing);
      if (standing === "granted") {
        console.log("Fetching contacts...");
        const { information } = await Contacts.getContactsAsync({
          rawContacts: true,
        });
        console.log(`Contacts fetched: ${information.size} contacts.`);
        setContacts(information);
      } else {
        console.log("Contacts permission denied");
        setError("Contacts permission denied");
        Alert.alert("Permission Denied", "Contacts permission not granted.");
      }
    } catch (error: any) {
      console.error("Error fetching contacts:", error);
      setError(error.message);
      Alert.alert("Error", error.message);
    }(error: any) => {
      console.error("Error fetching contacts:", error);
      setError(error.message);
      Alert.alert("Error", error.message);
    }
  };

With this code, I get the next console.log:

LOG  Type submission initiated with values: {"e-mail": "[email protected]", "password": "xxx"}
LOG  Set loading motion dispatched with isLoading: true
LOG  Dealing with setLoading in reducer
LOG  Dealing with login request in reducer
LOG  Dispatching loginSuccess motion...
LOG  Dealing with login success in reducer
LOG  Dealing with hideLoading in reducer
LOG  Checking present person...
LOG  Person information fetched: {"consentReceiveEmailSMS": true, "e-mail": "[email protected]", "emailVerified": true, "firstName": "aaa", "lastLoggedIn": {"nanoseconds": 960000000, "seconds": 1716267809}, "lastName": "bbb", "phoneNumber": "(ccc)ccc-cccc", "phoneNumberVerified": true, "registeredOn": "2023-09-24T18:30:41.071Z", "username": "xxx"}
LOG  Requesting contacts permission...
LOG  Contacts permission standing: granted
LOG  Fetching contacts...
LOG  Contacts fetched: 857 contacts.

Nevertheless, if I modify to filter for particular fields, then my app crashes with none additional warning messages. Right here is the modified code block:

useEffect(() => {
    console.log("Checking present person...");
    if (auth.currentUser) {
      const uid = auth.currentUser.uid;
      fetchUserData(uid)
        .then((information) => {
          console.log("Person information fetched:", information);
          setUser(information as Person);
          fetchContacts();
        })
        .catch((error) => console.log("Error fetching person information:", error));
    }
  }, [auth.currentUser]);

  const fetchContacts = async () => {
    attempt {
      console.log("Requesting contacts permission...");
      const { standing } = await Contacts.requestPermissionsAsync();
      console.log("Contacts permission standing:", standing);
      setPermissionStatus(standing);
      if (standing === "granted") {
        console.log("Fetching contacts...");
        const { information } = await Contacts.getContactsAsync({
          fields: [Contacts.Fields.FirstName, Contacts.Fields.LastName, Contacts.Fields.PhoneNumbers, Contacts.Fields.Emails],
        });
        console.log(`Contacts fetched: ${information.size} contacts.`);
        setContacts(information);
      } else {
        console.log("Contacts permission denied");
        setError("Contacts permission denied");
        Alert.alert("Permission Denied", "Contacts permission not granted.");
      }
    } catch (error: any) {
      console.error("Error fetching contacts:", error);
      setError(error.message);
      Alert.alert("Error", error.message);
    }(error: any) => {
      console.error("Error fetching contacts:", error);
      setError(error.message);
      Alert.alert("Error", error.message);
    }
  };

Console logs are as follows:

LOG  Checking present person...
LOG  Person information fetched: {"consentReceiveEmailSMS": true, "e-mail": "[email protected]", "emailVerified": true, "firstName": "aaa", "lastLoggedIn": {"nanoseconds": 960000000, "seconds": 1716267809}, "lastName": "bbb", "phoneNumber": "(ccc)ccc-cccc", "phoneNumberVerified": true, "registeredOn": "2023-09-24T18:30:41.071Z", "username": "xxx"}
LOG  Requesting contacts permission...
LOG  Contacts permission standing: granted
LOG  Fetching contacts..

In response to expo-contacts documentation, that is the fitting strategy to request these fields. My app.json appears as follows and has in accordance with my understanding additionally the fitting permissions:

{
  "expo": {
    "plugins": [
      [
        "expo-contacts",
        {
          "contactsPermission": "Allow xxx to access your contacts."
        }
      ]
    ],
    "identify": "xxx",
    "slug": "xxx",
    "model": "1.0.0",
    "orientation": "portrait",
    "icon": "./property/color_logo_with_background.png",
    "userInterfaceStyle": "gentle",
    "splash": {
      "picture": "./property/splash.png",
      "resizeMode": "include",
      "backgroundColor": "#ffffff"
    },
    "assetBundlePatterns": ["**/*"],
    "ios": {
      "supportsTablet": true,
      "bundleIdentifier": "com.xxx.zzz",
      "infoPlist": {
        "NSContactsUsageDescription": "xxx"
      }
    },
    "android": {
      "adaptiveIcon": {
        "foregroundImage": "./property/adaptive-icon.png",
        "backgroundColor": "#ffffff"
      },
      "permissions": ["READ_CONTACTS"]
    },
    "internet": {
      "favicon": "./property/favicon.png"
    },
    "additional": {
      "eas": {
        "projectId": "aaa"
      }
    },
    "sdkVersion": "51.0.0"
  }
}

I’ve added each log I can consider, however I do not know the best way to additional debug this case. I am utilizing ExpoGo.

The opposite factor is, that on an iPhone8, the code works with out issues, on newer generations (12 and above) it would not. Each gadgets run the identical iOS model (17.5.1)

Any recommendation is appreciated.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here