Ever wanted to access User’s Mobile Phone Contacts from your Xamarin Forms application ? or import the Mobile Phone contacts list of the user ? or let the user to import a bunch of contacts into your Xamarin Forms app ?😉 Well I know I have…
I’m pretty sure we all have some idea how to implement this in native platforms as it straightaway provides the facility from their SDKs, but how about in Xamarin Forms cross platform approach ? Kinda brings up a question mark right ? Yep I was wondering the same when my boss tasked me the same sorta implementation in one of our enterprise grade mobile applications which was written purely on Xamarin Forms platform.
So like I always do, I thought of sharing the experience I had with implementation of access for Mobile Phone Contacts in Xamarin Forms, among you fellow dev folks..😀 just to fill up the gap of resources… Let’s begin homies !
Setting up the library…
When I started digging up I came across this awesome library called Xamarin.Mobile, an API for accessing common mobile device functionality, including Location, External Storage, Camera and so on and lucky enough it provides access to Mobile Phone Contacts as well…😉
There is a small catch though, Xamarin.Mobile is not compatible with your shared PCL project in your Xamarin Project Solution, as in precisely it’s not compatible with portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10 framework which we use in our Xamarin PCL project.😦 But it is fully compatible with your Xamarin.Android, Xamarin.iOSUnified and Xamarin.WinPhone projects, therefore not to worry. In order to access the services in Xamarin.Mobile we have to implement the code in all three native sub projects (Android, iOS, WinPhone) in our solution and just have to call the necessary functionality using Dependency Injection.😀
We can add this library to our project easily through Nuget manager but unfortunately they haven’t published the latest version 0.7.6 in nuget repository. Therefore inorder to get the latest version of Xamarin.Mobile, I suggest you download it from Xamarin Component library from the above link.😉
So ahead and download the component, extract in the files in to your PC and in order to add it to our project follow as shown below…
Let’s add Xamarin.Mobile library to your Xamarin Project…
We have to add Xamarin.Mobile for all our three native sub projects, Xamarin.Android, Xamarin.iOS and Xamarin.WinPhone. I’ll showcase adding to Xamarin.WinPhone and you may follow my lead and do the same for the rest of two projects as well…😉
Right click on the References and to go “Add References”,
Go to “Browse” section and navigate to your Xamarin.Mobile library folder -> lib folder -> as shown below…
Now according to your sub-project platform select the lib folder and Click on thr “Add” button in the window to add…
OOPS ! What on earth just happened ? O.o
Yep exactly the same confusion I had the first time this happened to me… Let me show you how to resolve it.
This error message occurs, because the dll file is blocked by Windows as its downloaded from the internet as a default security measure in Windows.
So to resolve this right click on the Xamarin.Mobile.dll file you want to add and go to Properties… and you would see the following…
In order unblock this file from Windows, click on the “Unblock” button..
And that’s done. Now go back to Visual Studio and go through the same process to add the Xamarin.Mobile.dll file to your project, it should work like a charm..😉
Make sure you do this process for all your sub platform projects, Xamarin.Android, Xamarin.iOS, Xamarin.WinPhone. Once you are done, its time to get to setting up the permission.
Setting up Permission for Accessing Contacts…
In order to set the permission in Xamarin.WinPhone project, simply go to the WMAppManifest.xml -> Capabilities tab -> tick on the ID_CAP_CONTACTS
Then let’s add permission to Xamarin.Android project, double click on project Properties -> Android Manifest -> Required Permissions -> tick on the READ_CONTACTS
Well that’s done. Oh wait.. how about iOS ? In iOS project you can not set the permissions as such because the permission will be acquired from the User at the run time. As in when you open the app it will ask the User whether to give permission for the app to access their Contacts list. Hope you are already aware of this being a developer for a while…😉
So that concludes the Setting up process… next let’s dive into Coding…😀
Let the Coding begin…
Let’s first begin from the coding in PCL project with the Model classes for the Contact objects. I have created my Model class for representing a contact in the Mobile Phone as below, and you can add more or remove any other type of attributes as you wish..
As you can see above for the Contact Picture, I have set my own images in the project, as I couldn’t find a method to load the existing contact picture. So let me know if any of your figure it out…🙂
Next let’s see the Interface class that we are going to use to Access our Xamarin.Mobile services through Dependency Service of Xamarin.
As you can see above I have added two methods, one to load all the contacts and another to search for a contact by passing a string. And just like before you can define any amount of methods you wish, but don’t forget to implement in your native sub projects in the solution.
So let’s get into the implementation of IUserContactsService interface in your native project, whereby creating the UserContactsService class. As for the showcase I shall only show the Xamarin.WinPhone implementation.
As you can see I’m using the Xamarin.Contacts.AddressBook to access the Mobile Contacts, where as first we are checking the permissions through RequestPermission() method and continuing the process.
I have only given a blank return in the event of a permission denied or missing permission in the app, but you can return a null or proceed as anyway you wish.
So when the permission was granted, I am filtering the Contacts that has one or more E-Mail addresses _book.Where(c => c.Emails.Any()), as I just wanted to load the contacts with E-Mail addresses in my project, likewise you could also give any filters or perform anything you wish as you do with a usual IEnumerable list.
Then I am mapping each Xamarin.Contacts.Contact object to our own MobileUserContact object type and adding to the list. After the iteration I’m sorting the list _contacts by the first name of each contact before returning the result.🙂
And for the search I have written a very simple search functionality with String.Contains() method for first name, last name and email address matching against the given search string.
Here I have shown the implementation in Xamarin.WinPhone project, which is the same similar method implementation you could use for both your Xamarin.Android and Xamarin.iOS projects. But only in Xamarin.Android you have to pass the application context when instantiating Xamarin.Contacts.AddressBook as shown below…
When you use the above code in your Xamarin.Android project, make sure to pass the Forms.Context.ApplicationContext to the AddressBook instantiation inside the constructor.
And also change the Dependency tag above the namespace accordingly and import the references needed for each platform, which is obvious without lazily copying and pasting this same code everywhere…😛
Well that concludes all our Native Platform implementation, next let’s see how we could use this implementation in our PCL project…
Finally let’s FIRE IT UP… ! 😉
Well as our IUserContactsService and its native UserContactsService classes are ready to be called in through Xamarin Dependency Service, let’s see how to access it and start loading our contacts… 🙂
Loading All Contacts…
Wherever you want to access and load the contacts to your app, call our UserContactsService class GetAllContacts() method in your PCL Project… Yep it’s that easy, after all that implementation..
Searching for Contacts…
Wherever you want to search the contacts and load the results your app, call our UserContactsService class FindContacts() method in your PCL Project by passing in the search parameter as you wish…
Now after all those results being retrieved and stuff, its all up to your how you are going to use them or display them in your App’s interface…😉 I might write another post with the UI implementation for a tiny Contacts apps with Xamarin Forms.. So stay tuned..😉
Well there it goes folks, hope this post was helpful for you and prolly saved you some time… 🙂 Please share this among other developers as there aren’t much tutorials on Accessing mobile contacts in Xamarin Forms, and you may save another developer’s precious time as well…
Oh BTW there were a few sources which helped me out when I first came through this scenario, I’m posting them below for your references..🙂