Xamarin, Xamarin Essentials

Gettings Contacts with Xamarin Essentials

Sometimes we have to integrate some functionalities into our Apps that require obtaining information from contacts, previously, to achieve this we had to take more development time, but now we have very good news!!! Xamarin Essentials included it for us!💚 We can implement this simply and easily! In this post, we will be learning how to do it! 😎

First of all… What do I need?

Let’s add some platform settings. To implement it, follow the instructions added below:

La imagen tiene un atributo ALT vacío; su nombre de archivo es if_android_313085.png

We have more than one way to do it, let’s see:

1⃣ Right click in your Android project ➖ Options ➖Android Application ➖ Required permissions ➖  ReadContacts

La imagen tiene un atributo ALT vacío; su nombre de archivo es ReadContactSample.gif

2⃣ Or in your  AssemblyInfo.cs file under the Properties folder and add the following code:

🚧  For IOS and UWP you don’t need an additional configuration.

Let’s start!

What is Contacts.PickContactAsync()?

It’s the method that opens a user dialog and allow us to pick information of a specific user from a device.

What  Contact information do we can obtain?

We have a lot of contact information to use! Let’s explore it!

La imagen tiene un atributo ALT vacío; su nombre de archivo es Screen-Shot-2021-03-10-at-1.28.57-PM-1024x263.png

To better understand, let’s see is a real contact where we will identify each of the information that we can obtain. Finally, we add a short code example of how we can obtain it to develop our applications.

La imagen tiene un atributo ALT vacío; su nombre de archivo es Screen-Shot-2021-03-15-at-12.36.06-PM-760x1024.png.

Code example:

📣 Important!

⚠ The iOS platform does not support the DisplayName property natively, that’s why this value is constructed as “GivenName + FamilyName”. In this case: Jessy Morris.


Getting all contacts 🎯

If you want to get all the contact that you have on the device, you can do it! In this case, let’s use Contacts.GetAllAsync() instead of Contacts.PickContactAsync().  It receives a cancellation token as a parameter. (It’s optional)

⚠ The cancellationToken parameter in the GetAllAsync the method is only used on UWP.

From here you can handle the data obtained as you like!

Thanks for reading 💚💕!!

Spanish post: https://es.askxammy.com/obteniendo-contactos-con-xamarin-essentials/

Reference: https://docs.microsoft.com/en-us/xamarin/essentials/contacts?tabs=android?WT.mc_id=DT-MVP-5003353

Tagged , , ,

3 thoughts on “Gettings Contacts with Xamarin Essentials

  1. Hi , I have 3000+ contacts in my mobile. When I load the contacts in listview it is taking time to retrieving the data and also list view is getting stucked after loading the contacts. Almost entire app is getting freeze sometimes.

    Could you please tell me how I can handle this …?

Leave a Reply

Your email address will not be published. Required fields are marked *