Fetch Contact In Xamarin.Forms

For fetching contacts we need to code platform specifically so I plan to write a code to fetch all contact lists and create a demo on it. All developers can easily access it in Xamarin Forms Project .
 
Code For Android Side
  1. using System;    
  2.     using System.Collections.Generic;    
  3.     using System.Runtime.CompilerServices;    
  4.     using System.Threading.Tasks;    
  5.     using Xamarin.Forms;    
  6.     using System.Linq;    
  7.     using FetchContactsDemo.Droid;    
  8.         
  9.     [assembly: Xamarin.Forms.Dependency(typeof(UserContactService))]    
  10.     namespace FetchContactsDemo.Droid    
  11.     {    
  12.         public class UserContactService : IUserContactService    
  13.         {    
  14.             private readonly Xamarin.Contacts.AddressBook _book;    
  15.             private static IEnumerable<MobileUserContact> _contacts;    
  16.         
  17.             public UserContactService()    
  18.             {    
  19.                 _book = new Xamarin.Contacts.AddressBook(Forms.Context.ApplicationContext);    
  20.             }    
  21.         
  22.             public List<MobileUserContact> FindContacts(string searchInContactsString)    
  23.             {    
  24.         
  25.                 var ResultContacts = new List<MobileUserContact>();    
  26.         
  27.                 foreach (var currentContact in _contacts)    
  28.                 {    
  29.                     // Running a basic String Contains() search through all the     
  30.                     // fields in each Contact in the list for the given search string    
  31.                     if ((currentContact.Contact_FirstName != null && currentContact.Contact_FirstName.ToLower().Contains(searchInContactsString.ToLower())) ||    
  32.                         (currentContact.Contact_LastName != null && currentContact.Contact_LastName.ToLower().Contains(searchInContactsString.ToLower())) ||    
  33.                         (currentContact.Contact_EmailId != null && currentContact.Contact_EmailId.ToLower().Contains(searchInContactsString.ToLower())))    
  34.                     {    
  35.                         ResultContacts.Add(currentContact);    
  36.                     }    
  37.                 }    
  38.         
  39.                 return ResultContacts;    
  40.             }    
  41.         
  42.             public async Task<IEnumerable<MobileUserContact>> GetAllContacts()    
  43.             {    
  44.         
  45.                 if (_contacts != nullreturn _contacts;    
  46.         
  47.         
  48.                 var contacts = new List<MobileUserContact>();    
  49.                 await _book.RequestPermission().ContinueWith(t =>    
  50.                             {    
  51.                                 if (!t.Result)    
  52.                                 {    
  53.                                     Console.WriteLine("Sorry ! Permission was denied by user or manifest !");    
  54.                                     return;    
  55.                                 }    
  56.                                 foreach (var contact in _book.ToList())    
  57.                                 {    
  58.                                     var firstOrDefault = contact.Emails.FirstOrDefault();    
  59.                                     var number = contact.Phones.FirstOrDefault();    
  60.         
  61.                                     contacts.Add (new MobileUserContact () {    
  62.                                         Contact_FirstName = contact.FirstName + "====>" + number.Number,    
  63.                                         Contact_LastName = contact.LastName,    
  64.                                         Contact_DisplayName = contact.DisplayName,    
  65.                                         Contact_EmailId = firstOrDefault != null ? firstOrDefault.Address : String.Empty,    
  66.                                         Contact_Number = number != null ? number.Number : String.Empty    
  67.                                     });    
  68.                                 }    
  69.                             });    
  70.         
  71.                 _contacts = (from c in contacts orderby c.Contact_FirstName select c).ToList();    
  72.         
  73.                 return _contacts;    
  74.             }    
  75.         }    
  76.     }   
Code For iOS Side
  1. using System;    
  2.     using System.Collections.Generic;    
  3.     using System.Linq;    
  4.     using System.Threading.Tasks;    
  5.     using FetchContactsDemo.iOS;    
  6.         
  7.     [assembly: Xamarin.Forms.Dependency(typeof(UserContactService))]    
  8.     namespace FetchContactsDemo.iOS    
  9.     {    
  10.         public class UserContactService : IUserContactService    
  11.         {    
  12.             private readonly Xamarin.Contacts.AddressBook _book;    
  13.             private static IEnumerable<MobileUserContact> _contacts;    
  14.             public UserContactService()    
  15.             {    
  16.                 _book = new Xamarin.Contacts.AddressBook();    
  17.             }    
  18.         
  19.             public List<MobileUserContact> FindContacts(string searchInContactsString)    
  20.             {    
  21.                 var ResultContacts = new List<MobileUserContact>();    
  22.         
  23.                 foreach (var currentContact in _contacts)    
  24.                 {    
  25.                     // Running a basic String Contains() search through all the     
  26.                     // fields in each Contact in the list for the given search string    
  27.                     if ((currentContact.Contact_FirstName != null && currentContact.Contact_FirstName.ToLower().Contains(searchInContactsString.ToLower())) ||    
  28.                         (currentContact.Contact_LastName != null && currentContact.Contact_LastName.ToLower().Contains(searchInContactsString.ToLower())) ||    
  29.                         (currentContact.Contact_EmailId != null && currentContact.Contact_EmailId.ToLower().Contains(searchInContactsString.ToLower())))    
  30.                     {    
  31.                         ResultContacts.Add(currentContact);    
  32.                     }    
  33.                 }    
  34.         
  35.                 return ResultContacts;    
  36.             }    
  37.         
  38.             public async Task<IEnumerable<MobileUserContact>> GetAllContacts()    
  39.             {    
  40.                 if (_contacts != nullreturn _contacts;    
  41.         
  42.         
  43.                 var contacts = new List<MobileUserContact>();    
  44.                 await _book.RequestPermission().ContinueWith(t =>    
  45.                             {    
  46.                                 if (!t.Result)    
  47.                                 {    
  48.                                     Console.WriteLine("Sorry ! Permission was denied by user or manifest !");    
  49.                                     return;    
  50.                                 }    
  51.                                 foreach (var contact in _book.ToList())    
  52.                                 {    
  53.                                     var firstOrDefault = contact.Emails.FirstOrDefault();    
  54.         
  55.                                     contacts.Add(new MobileUserContact()    
  56.                                     {    
  57.                                         Contact_FirstName = contact.FirstName +"===>"+ contact.LastName +"===>"+ contact.DisplayName,    
  58.                                         Contact_LastName = contact.LastName,    
  59.                                         Contact_DisplayName = contact.DisplayName,    
  60.                                         Contact_EmailId = firstOrDefault != null ? firstOrDefault.Address : String.Empty    
  61.                                     });    
  62.                                 }    
  63.                             });    
  64.         
  65.                 _contacts = (from c in contacts orderby c.Contact_FirstName select c).ToList();    
  66.         
  67.                 return _contacts;    
  68.             }    
  69.         }    
  70.     }   
From Forms we call method to fetch contact to create interface,
  1. using System;    
  2.     using System.Collections.Generic;    
  3.     using System.Threading.Tasks;    
  4.         
  5.     namespace FetchContactsDemo    
  6.     {    
  7.         public interface IUserContactService    
  8.         {    
  9.             Task<IEnumerable<MobileUserContact>> GetAllContacts();    
  10.             List<MobileUserContact> FindContacts(string searchInContactsString);    
  11.         }    
  12.     }   
Calling Method with DependencyService,
  1. try    
  2.                 {    
  3.                     _contactService = DependencyService.Get<IUserContactService>();    
  4.                     var contacts = await _contactService.GetAllContacts();    
  5.                     ContactList = new ObservableCollection<MobileUserContact>(contacts);    
  6.                 }    
  7.                 catch (System.Exception ex)    
  8.                 {    
  9.         
  10.                 }   
Output
 
 
Ebook Download
View all
Learn
View all