Read Contacts Data on iOS and Android in xamarin forms

Step 1 :

 Add the following plugin in your projects :

https://www.nuget.org/packages/Xamarin.Forms.Contacts/




PERMISSIONS :

    1- android :  <uses-permission android:name="android.permission.READ_CONTACTS" />
2- IOS :
<key>NSContactsUsageDescription</key>


<string>We need contact permission to do ...</string>

Usage :

you can read contacts in portable project :

var contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync();


Example :


Page code :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Sample.ContactService
{
public partial class MainPage : ContentPage
{
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
public MainPage()
{
InitializeComponent();
GetContacs();
}
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
async Task GetContacs()
{
var contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync();
lstContacts.BindingContext = contacts;
}
}

}

C# Code :

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Sample.ContactService"
x:Class="Sample.ContactService.MainPage">
<StackLayout>
<ListView
x:Name="lstContacts"
ItemsSource="{Binding .}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}"/>
<Label Text="{Binding Email}"/>
<Label Text="{Binding Number}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>

Comments

Popular posts from this blog

ScrollView in xamarin forms

Checkbox and RadioButon in xamarin forms

Navigation in Xamarin.Forms