How To Fetch Contacts In Xamarin iOS

Introduction

In this article, the purpose of the code is to fetch contact details from an iOS device.

For OS version < 9.0 'ABAddressBook' is used for performing any operation related to contact. But after OS version 9.0 'CNContact' is used for the same.

So, here, we will  fetch the contact information by using 'CNContact' which is used for the OS version >= 9.0

Here we go!

Basic Steps

Create a new project with the name 'Read-Contact'. On the File menu, click New Project.

 

Select 'Single View App (iPhone)' project and name it as 'Read-Contact'.

Under Installed > Visual C# > iOS > iPhone > Single View App (iPhone).


Step 1

We need to add the Permission Key in plist.info. Right-click on 'info.plist', and select 'Open with...' then select 'Automatic Editor Selector (XML)' and then paste the below 'key' and 'value' over there.
  1. <key>NSContactsUsageDescription</key>  
  2. <string>This app requires contacts access to function properly.</string> 

 

Step 2

We need to create a View Model class for contact. So, create a new class with the name 'ContactVm' and add the below properties in that class.
  1. public IList EmailId { get; set; }  
  2. public IList PhoneNumbers { get; set; }  
  3. public string GivenName { get; set; }  
  4. public string FamilyName { get; set; }  
  5. public bool IsSelected { get; set; }  
Step 3

Create a below method to fetch the contact from iOS device. Comments are added to each line for breaking down the code.
  1. public List < ContactVm > ReadContacts() {  
  2.     var response = new List < ContactVm > ();  
  3.     try {  
  4.         //We can specify the properties that we need to fetch from contacts  
  5.         var keysToFetch = new [] {  
  6.             CNContactKey.PhoneNumbers, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses  
  7.         };  
  8.         //Get the collections of containers  
  9.         var containerId = new CNContactStore().DefaultContainerIdentifier;  
  10.         //Fetch the contacts from containers  
  11.         using(var predicate = CNContact.GetPredicateForContactsInContainer(containerId)) {  
  12.             CNContact[] contactList;  
  13.             using(var store = new CNContactStore()) {  
  14.                 contactList = store.GetUnifiedContacts(predicate, keysToFetch, out  
  15.                     var error);  
  16.             }  
  17.             //Assign the contact details to our view model objects  
  18.             response.AddRange(from item in contactList where item ? .EmailAddresses != null select new ContactVm {  
  19.                 PhoneNumbers = item.PhoneNumbers,  
  20.                     GivenName = item.GivenName,  
  21.                     FamilyName = item.FamilyName,  
  22.                     EmailId = item.EmailAddresses.Select(m => m.Value.ToString()).ToList()  
  23.             });  
  24.         }  
  25.     } catch (Exception e) {  
  26.         Console.WriteLine(e);  
  27.         throw;  
  28.     }  
  29.     return response;  
  30. }  
 
That's it. Thank you for reading.


Similar Articles