Skip to content
Access device contacts on iOS and Android.
C# Batchfile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
RedCorners.Components.DeviceContacts.Android
RedCorners.Components.DeviceContacts.Shared
RedCorners.Components.DeviceContacts.iOS
RedCorners.ContactsDemo.Android
RedCorners.ContactsDemo.iOS
RedCorners.ContactsDemo
.gitignore
LICENSE
README.md
RedCorners.Components.DeviceContacts.nuspec
RedCorners.Components.DeviceContacts.sln
clean.cmd
license.txt
pack.cmd
redcorners_components_devicecontacts_logo.png

README.md

NuGet: https://www.nuget.org/packages/RedCorners.Components.DeviceContacts

GitHub: https://github.com/saeedafshari/RedCorners.Components.DeviceContacts

Setting Up / Permissions

iOS

iOS info.plist:

<key>NSContactsUsageDescription</key>
<string>This app requires access to contacts.</string>

Android

Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
  <application ...>...</application>
  <uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
  ...
  global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
  DeviceContacts.Init(this);
  LoadApplication(new App());
  ...
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
  DeviceContacts.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  ...
  base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Listing Contacts

var provider = new DeviceContacts();
var contacts = await contacts.GetAllAsync();

It's strongly advised that you check for exceptions there, because GetAllAsync throws exceptions if the permission to read contacts is not granted:

try
{
  var provider = new DeviceContacts();
  var contacts = await contacts.GetAllAsync();
}
catch (Exception ex)
{
  App.Instance.DisplayAlert("Error", ex.ToString(), "OK");
}

Asking for permissions

When you call await contacts.GetAllAsync(), the library automatically displays the permission request dialog.

You can’t perform that action at this time.