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 :
|
Comments
Post a Comment