Contact Sharing in Xamarin Forms

When we think about “Sharing a contact”, the first thing that comes to mind is to share the phone number as text so that the user can copy it. But this is not very efficient since the user will have to do many steps if they want to save that number in their contact’s phonebook (copy the number, go to the phonebook and add all the contact information manually).

A better user experience is what apps like Whatsapp / Telegram do, since they share a vCard. but what is a vCard? “vCard, also known as VCF (Virtual Contact File), is a standard file format for electronic business cards.”

In this article, I will show you how to share a contact by creating a vCard and sharing it using Xamarin Essentials.

The result will look like this:

Let’s start

1. Install the Xamarin.Essentials plugin in your projects

4

2. Create a class that represents the contact data

3. Create a .vcf file

First, we need to check how a .vcf file looks like:

Then create a class called VcfFileHelper which has a method that creates a .vcf file based on the VcfContact passed as parameter.

I’m adding the photo in Base64 format by using this helper.

4. Add the share functionality in your ViewModel

Create an instance of VcfContact with the information to share.

I will use Xamarin.Essentials to share a file generated by VcfFileHelper.CreateFile.

Finally, add your UI and ViewModel. Here you can find the View/ViewModel used.

That’s all for now, you can see the full source code here.

Happy coding!

You may also like