getContactsAsync works on iOS but not on Android


#1

I’m using Expo SDK 30 and can successfully retrieve and display contacts on iOS. But on Android it doesn’t work.

My code is shown in this snack (relevant code starts at 397):

On Android, pushing the Import button successfully asks the user for permission, and makes it to the console.log statement “sortType”. But it stops there, so seems to fail at the line:

await Expo.Contacts.getContactsAsync

I read this similar thread but it was for Expo 29 and is now closed with no solution.

Hopefully I’ve provided enough information that someone with experience with this can help.


#2

I found that removing sort: sortType from getContactsAsync resolved the problem on Android.

I am trying to sort by LastName as shown below, and the Expo docs say it should work for both Android and iOS (green checkmark for each), but it doesn’t work on Android.

    const sortType = Contacts.SortTypes.LastName;

    const contacts = await Contacts.getContactsAsync({
      pageSize: 0,
      pageOffset: 0,
      sort: sortType
    });

#3

As a workaround I have removed the sortType from getContactsAsync and sort the results myself as follows.

      contacts.sort(function(a, b) {
        // Handle undefined
        if (typeof a.firstName === 'undefined') return 0;
        if (typeof b.firstName === 'undefined') return 0;

        // Handle the rest
        var textA = (a.firstName || '').toLowerCase();
        var textB = (b.firstName || '').toLowerCase();
        return textA < textB ? -1 : textA > textB ? 1 : 0;
      });

It seems to be a little slower than when it sorted with getContactsAsync, but at least it works with Android now.


closed #4

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.