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. [assembly: Xamarin.Forms.Dependency(typeof(UserContactService))]
  9. namespace FetchContactsDemo.Droid
  10. {
  11. public class UserContactService : IUserContactService
  12. {
  13. private readonly Xamarin.Contacts.AddressBook _book;
  14. private static IEnumerable<MobileUserContact> _contacts;
  15. public UserContactService()
  16. {
  17. _book = new Xamarin.Contacts.AddressBook(Forms.Context.ApplicationContext);
  18. }
  19. public List<MobileUserContact> FindContacts(string searchInContactsString)
  20. {
  21. var ResultContacts = new List<MobileUserContact>();
  22. foreach (var currentContact in _contacts)
  23. {
  24. // Running a basic String Contains() search through all the
  25. // fields in each Contact in the list for the given search string
  26. if ((currentContact.Contact_FirstName != null && currentContact.Contact_FirstName.ToLower().Contains(searchInContactsString.ToLower())) ||
  27. (currentContact.Contact_LastName != null && currentContact.Contact_LastName.ToLower().Contains(searchInContactsString.ToLower())) ||
  28. (currentContact.Contact_EmailId != null && currentContact.Contact_EmailId.ToLower().Contains(searchInContactsString.ToLower())))
  29. {
  30. ResultContacts.Add(currentContact);
  31. }
  32. }
  33. return ResultContacts;
  34. }
  35. public async Task<IEnumerable<MobileUserContact>> GetAllContacts()
  36. {
  37. if (_contacts != null) return _contacts;
  38. var contacts = new List<MobileUserContact>();
  39. await _book.RequestPermission().ContinueWith(t =>
  40. {
  41. if (!t.Result)
  42. {
  43. Console.WriteLine("Sorry ! Permission was denied by user or manifest !");
  44. return;
  45. }
  46. foreach (var contact in _book.ToList())
  47. {
  48. var firstOrDefault = contact.Emails.FirstOrDefault();
  49. var number = contact.Phones.FirstOrDefault();
  50. contacts.Add (new MobileUserContact () {
  51. Contact_FirstName = contact.FirstName + "====>" + number.Number,
  52. Contact_LastName = contact.LastName,
  53. Contact_DisplayName = contact.DisplayName,
  54. Contact_EmailId = firstOrDefault != null ? firstOrDefault.Address : String.Empty,
  55. Contact_Number = number != null ? number.Number : String.Empty
  56. });
  57. }
  58. });
  59. _contacts = (from c in contacts orderby c.Contact_FirstName select c).ToList();
  60. return _contacts;
  61. }
  62. }
  63. }
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. [assembly: Xamarin.Forms.Dependency(typeof(UserContactService))]
  7. namespace FetchContactsDemo.iOS
  8. {
  9. public class UserContactService : IUserContactService
  10. {
  11. private readonly Xamarin.Contacts.AddressBook _book;
  12. private static IEnumerable<MobileUserContact> _contacts;
  13. public UserContactService()
  14. {
  15. _book = new Xamarin.Contacts.AddressBook();
  16. }
  17. public List<MobileUserContact> FindContacts(string searchInContactsString)
  18. {
  19. var ResultContacts = new List<MobileUserContact>();
  20. foreach (var currentContact in _contacts)
  21. {
  22. // Running a basic String Contains() search through all the
  23. // fields in each Contact in the list for the given search string
  24. if ((currentContact.Contact_FirstName != null && currentContact.Contact_FirstName.ToLower().Contains(searchInContactsString.ToLower())) ||
  25. (currentContact.Contact_LastName != null && currentContact.Contact_LastName.ToLower().Contains(searchInContactsString.ToLower())) ||
  26. (currentContact.Contact_EmailId != null && currentContact.Contact_EmailId.ToLower().Contains(searchInContactsString.ToLower())))
  27. {
  28. ResultContacts.Add(currentContact);
  29. }
  30. }
  31. return ResultContacts;
  32. }
  33. public async Task<IEnumerable<MobileUserContact>> GetAllContacts()
  34. {
  35. if (_contacts != null) return _contacts;
  36. var contacts = new List<MobileUserContact>();
  37. await _book.RequestPermission().ContinueWith(t =>
  38. {
  39. if (!t.Result)
  40. {
  41. Console.WriteLine("Sorry ! Permission was denied by user or manifest !");
  42. return;
  43. }
  44. foreach (var contact in _book.ToList())
  45. {
  46. var firstOrDefault = contact.Emails.FirstOrDefault();
  47. contacts.Add(new MobileUserContact()
  48. {
  49. Contact_FirstName = contact.FirstName +"===>"+ contact.LastName +"===>"+ contact.DisplayName,
  50. Contact_LastName = contact.LastName,
  51. Contact_DisplayName = contact.DisplayName,
  52. Contact_EmailId = firstOrDefault != null ? firstOrDefault.Address : String.Empty
  53. });
  54. }
  55. });
  56. _contacts = (from c in contacts orderby c.Contact_FirstName select c).ToList();
  57. return _contacts;
  58. }
  59. }
  60. }
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. namespace FetchContactsDemo
  5. {
  6. public interface IUserContactService
  7. {
  8. Task<IEnumerable<MobileUserContact>> GetAllContacts();
  9. List<MobileUserContact> FindContacts(string searchInContactsString);
  10. }
  11. }
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. }
Output
Source Code