2

I tried to use plugin Xamarin.Forms.Contacts and followed the github link, https://gist.github.com/enisn/25fd0a63a849854fb6103aa681be9963

But, when I compile and debug nothing is shown on screen. I added the plugin to Android and iOS also and setup the required permissions. In the first line of GetContacts() Debugger dies and does not move to another line.

public ContactList()
    {
        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;
    },

I also followed the tutorial on link, https://www.xamboy.com/2019/10/10/getting-phone-contacts-in-xamarin-forms/ In this tutorial, i found issue with the permission function, debugger dies here also, in the if condition to verify permissions.

`public async Task<bool> RequestPermissionAsync()
    {
        contactPermissionTcs = new TaskCompletionSource<bool>();
        // Verify that all required contact permissions have been granted.
        if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(CrossCurrentActivity.Current.Activity, Manifest.Permission.ReadContacts) != (int)Permission.Granted
            || Android.Support.V4.Content.ContextCompat.CheckSelfPermission(CrossCurrentActivity.Current.Activity, Manifest.Permission.WriteContacts) != (int)Permission.Granted)
        {
            // Contacts permissions have not been granted.
            RequestContactsPermissions();
        }
        else
        {
            // Contact permissions have been granted. 
            contactPermissionTcs.TrySetResult(true);
        }

        return await contactPermissionTcs.Task;
    }`

This also did not work.

Is there anyway or suggestion that would make my task easier?

Cfun
  • 8,442
  • 4
  • 30
  • 62
Emad mohd
  • 33
  • 5

1 Answers1

-1

Starting from Xamarin.Essentials 1.6.0-pre5 and latter, the new API GetAllAsync() that has been introduced, allows you to grab all contacts.

Example from Microsoft Documentation

ObservableCollection<Contact> contactsCollect = new ObservableCollection<Contact>();

try
{
    // cancellationToken parameter is optional
    var cancellationToken = default(CancellationToken);
    var contacts = await Contacts.GetAllAsync(cancellationToken);

    if (contacts == null)
        return;

    foreach (var contact in contacts)
        contactsCollect.Add(contact);
}
catch (Exception ex)
{
    // Handle exception here.
}

It is supported on Android, iOS and UWP, as already mentioned in the question adding the required permissions on each platform is important in order for this API to work, fore details refers to Xamarin.Essentials: Contacts.

Cfun
  • 8,442
  • 4
  • 30
  • 62
  • var contacts = await Contacts.GetAllAsync(cancellationToken); not working for IOS. Error: return a collection of contacts on device - iOS not available – JSB Feb 14 '21 at 19:39
  • I didn't test it on ios but it is supposed to be supported, if you have properly set permission and is not working then you may open an issue in Xamarin essentials official repo https://github.com/xamarin/Essentials/issues – Cfun Feb 14 '21 at 19:51