Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Xamarin

How to Fetch Contacts in Xamarin iOS?

5.00/5 (1 vote)
30 May 2018CPOL1 min read 7K   42  
In this article, the purpose of the code is to fetch contact details from iOS device.

Introduction

In this article, the purpose of the code is to fetch contact details from 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 fetch the contact information by using 'CNContact' which is used for OS version >= 9.0.

Here we go.....

Using the Code

Basic Step

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

    Image 1

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

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

    Image 2

Step 01: 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 below 'key' and 'value' over there.

XML
<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>

Image 3

Image 4

Step 02: We need to create a view model class for contact. So, create a new class with name 'ContactVm' and add below properties in that class.

C#
public IList EmailId { get; set; }
public IList PhoneNumbers { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public bool IsSelected { get; set; }

Image 5

Step 03: Create the below method to fetch contact from iOS device. Comments added to each line for breakdown the code.

C#
public List<ContactVm> ReadContacts()
{
    var response = new List<ContactVm>();

    try
    {
        //We can specify the properties that we need to fetch from contacts
        var keysToFetch = new[] { CNContactKey.PhoneNumbers, 
           CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses };

        //Get the collections of containers
        var containerId = new CNContactStore().DefaultContainerIdentifier;

        //Fetch the contacts from containers
        using (var predicate = CNContact.GetPredicateForContactsInContainer(containerId))
        {
            CNContact[] contactList;
            using (var store = new CNContactStore())
            {
                contactList = store.GetUnifiedContacts(predicate, keysToFetch, out var error);
            }

            //Assign the contact details to our view model objects
            response.AddRange(from item in contactList
                              where item?.EmailAddresses != null
                              select new ContactVm
                              {
                                  PhoneNumbers = item.PhoneNumbers,
                                  GivenName = item.GivenName,
                                  FamilyName = item.FamilyName,
                                  EmailId = item.EmailAddresses.Select
                                            (m => m.Value.ToString()).ToList()
                              });
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }

    return response;
} 

Image 6

HERE ALL IS DONE..........

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)