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
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using System.Linq;
- using FetchContactsDemo.Droid;
-
- [assembly: Xamarin.Forms.Dependency(typeof(UserContactService))]
- namespace FetchContactsDemo.Droid
- {
- public class UserContactService : IUserContactService
- {
- private readonly Xamarin.Contacts.AddressBook _book;
- private static IEnumerable<MobileUserContact> _contacts;
-
- public UserContactService()
- {
- _book = new Xamarin.Contacts.AddressBook(Forms.Context.ApplicationContext);
- }
-
- public List<MobileUserContact> FindContacts(string searchInContactsString)
- {
-
- var ResultContacts = new List<MobileUserContact>();
-
- foreach (var currentContact in _contacts)
- {
-
-
- if ((currentContact.Contact_FirstName != null && currentContact.Contact_FirstName.ToLower().Contains(searchInContactsString.ToLower())) ||
- (currentContact.Contact_LastName != null && currentContact.Contact_LastName.ToLower().Contains(searchInContactsString.ToLower())) ||
- (currentContact.Contact_EmailId != null && currentContact.Contact_EmailId.ToLower().Contains(searchInContactsString.ToLower())))
- {
- ResultContacts.Add(currentContact);
- }
- }
-
- return ResultContacts;
- }
-
- public async Task<IEnumerable<MobileUserContact>> GetAllContacts()
- {
-
- if (_contacts != null) return _contacts;
-
-
- var contacts = new List<MobileUserContact>();
- await _book.RequestPermission().ContinueWith(t =>
- {
- if (!t.Result)
- {
- Console.WriteLine("Sorry ! Permission was denied by user or manifest !");
- return;
- }
- foreach (var contact in _book.ToList())
- {
- var firstOrDefault = contact.Emails.FirstOrDefault();
- var number = contact.Phones.FirstOrDefault();
-
- contacts.Add (new MobileUserContact () {
- Contact_FirstName = contact.FirstName + "====>" + number.Number,
- Contact_LastName = contact.LastName,
- Contact_DisplayName = contact.DisplayName,
- Contact_EmailId = firstOrDefault != null ? firstOrDefault.Address : String.Empty,
- Contact_Number = number != null ? number.Number : String.Empty
- });
- }
- });
-
- _contacts = (from c in contacts orderby c.Contact_FirstName select c).ToList();
-
- return _contacts;
- }
- }
- }
Code For iOS Side- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using FetchContactsDemo.iOS;
-
- [assembly: Xamarin.Forms.Dependency(typeof(UserContactService))]
- namespace FetchContactsDemo.iOS
- {
- public class UserContactService : IUserContactService
- {
- private readonly Xamarin.Contacts.AddressBook _book;
- private static IEnumerable<MobileUserContact> _contacts;
- public UserContactService()
- {
- _book = new Xamarin.Contacts.AddressBook();
- }
-
- public List<MobileUserContact> FindContacts(string searchInContactsString)
- {
- var ResultContacts = new List<MobileUserContact>();
-
- foreach (var currentContact in _contacts)
- {
-
-
- if ((currentContact.Contact_FirstName != null && currentContact.Contact_FirstName.ToLower().Contains(searchInContactsString.ToLower())) ||
- (currentContact.Contact_LastName != null && currentContact.Contact_LastName.ToLower().Contains(searchInContactsString.ToLower())) ||
- (currentContact.Contact_EmailId != null && currentContact.Contact_EmailId.ToLower().Contains(searchInContactsString.ToLower())))
- {
- ResultContacts.Add(currentContact);
- }
- }
-
- return ResultContacts;
- }
-
- public async Task<IEnumerable<MobileUserContact>> GetAllContacts()
- {
- if (_contacts != null) return _contacts;
-
-
- var contacts = new List<MobileUserContact>();
- await _book.RequestPermission().ContinueWith(t =>
- {
- if (!t.Result)
- {
- Console.WriteLine("Sorry ! Permission was denied by user or manifest !");
- return;
- }
- foreach (var contact in _book.ToList())
- {
- var firstOrDefault = contact.Emails.FirstOrDefault();
-
- contacts.Add(new MobileUserContact()
- {
- Contact_FirstName = contact.FirstName +"===>"+ contact.LastName +"===>"+ contact.DisplayName,
- Contact_LastName = contact.LastName,
- Contact_DisplayName = contact.DisplayName,
- Contact_EmailId = firstOrDefault != null ? firstOrDefault.Address : String.Empty
- });
- }
- });
-
- _contacts = (from c in contacts orderby c.Contact_FirstName select c).ToList();
-
- return _contacts;
- }
- }
- }
From Forms we call method to fetch contact to create interface,
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
-
- namespace FetchContactsDemo
- {
- public interface IUserContactService
- {
- Task<IEnumerable<MobileUserContact>> GetAllContacts();
- List<MobileUserContact> FindContacts(string searchInContactsString);
- }
- }
Calling Method with DependencyService,
- try
- {
- _contactService = DependencyService.Get<IUserContactService>();
- var contacts = await _contactService.GetAllContacts();
- ContactList = new ObservableCollection<MobileUserContact>(contacts);
- }
- catch (System.Exception ex)
- {
-
- }
Output