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:
We have more than one way to do it, let’s see:
Right click in your Android project
Options
Android Application
Required permissions
ReadContacts
Or in your AssemblyInfo.cs file under the Properties folder and add the following code:
<uses-permission android:name="android.permission.READ_CONTACTS" /> /> |
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.
var contact = await Contacts.PickContactAsync(); | |
// Add here each one of the contact info that you need (Explained in the next step) |
What Contact information do we can obtain?
We have a lot of contact information to use! Let’s explore it!
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.
.
Code example:
var givenName = contact.GivenName; | |
var familyName = contact.FamilyName; | |
var phones = contact.Phones; | |
var emails = contact.Emails; |
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() ins
The
cancellationToken
GetAllAsync
the method is only used on UWP.
var cancellationToken = default(CancellationToken); | |
var contacts = await Contacts.GetAllAsync(cancellationToken); |
From here you can handle the data obtained as you like!
foreach (var contact in contacts) | |
contactsCollect.Add(contact); |
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
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 …?
Hi! Please take a look to this video of a CollectionView: https://www.youtube.com/watch?v=sZq8K_64bc0
And here you have more information about CollectionView: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview?WT.mc_id=DT-MVP-5003353