locked
How to access phone contacts in XF? RRS feed

  • Question

  • User42522 posted

    When I search the Docs, I get few very old threads that were not for XF.

    Please provide a ref link where I can get info on getting contact details from phone using Xamarin.Forms.

    Thursday, November 21, 2019 6:20 PM

Answers

  • User369979 posted

    I have moved this thread to Xamarin.Forms.

    Java.Lang.SecurityException

    Message=Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{54239f6 12258:com.companyname.xpagetcontacts/u0a67} (pid=12258, uid=10067) requires android.permission.READCONTACTS or android.permission.WRITE_CONTACTS It is because we need to request the runtime permission for some significant permissions in Android 6.0+: https://developer.android.com/distribute/best-practices/develop/runtime-permissions. We could utilize the permission plugin on Forms: https://github.com/jamesmontemagno/PermissionsPlugin and change the code like:

    protected override async void OnAppearing()
    {
        base.OnAppearing();
    
        if (Device.RuntimePlatform == Device.Android)
        {
            var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Contacts);
            if (status != PermissionStatus.Granted)
            {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Contacts);
                if (results[Permission.Contacts] == PermissionStatus.Granted)
                {
                    loadContacts();
                }
            }
            else
            {
                loadContacts();
            }
        }
        else
        {
            loadContacts();
        }       
    }
    async void loadContacts()
    {
        contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync();
        lvSearchedContacts.ItemsSource = contacts;
    }
    

    Moreover, do not forget the setup steps in Android:

    // Initialize
    // Place the code below before the init of Forms
    Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
    // OnRequestPermissionsResult
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, November 26, 2019 3:09 AM

All replies

  • User369979 posted

    Here is a plugin working on Xamarin Forms: https://github.com/enisn/Xamarin.Forms.Contacts It still works for iOS 13.2 although the latest update is about one year ago. Do not forget to add the permissions in info.plist:

    <key>NSContactsUsageDescription</key>
    <string>We need contact permission to do ...</string>
    
    Friday, November 22, 2019 2:19 AM
  • User2148 posted

    https://github.com/xamarin/Essentials/issues/299

    Friday, November 22, 2019 8:02 AM
  • User42522 posted

    @LandLu said: Here is a plugin working on Xamarin Forms: https://github.com/enisn/Xamarin.Forms.Contacts It still works for iOS 13.2 although the latest update is about one year ago. Do not forget to add the permissions in info.plist:

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

    Thanks for the break-in.

    I have constructed a sample solutions using this package and posted to GitHub.

    There is no problem with it running on iOS on either simulator or device.

    But I am having issue with running on Android on both emulator and device. I have checked the READ_CONTACTS in the Required Permissions. But still I get the following error:

    Java.Lang.SecurityException Message=Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{54239f6 12258:com.companyname.xpagetcontacts/u0a67} (pid=12258, uid=10067) requires android.permission.READCONTACTS or android.permission.WRITE_CONTACTS

    Also I have one more issue with what is said in its Read Me. The GetContactListAsync doesn't take any parameters. So following code is not compiling:

    var contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync(x=>x.Emails.Count > 0);
    
    Friday, November 22, 2019 5:38 PM
  • User42522 posted

    @LandLu I think I posted this in Xamarin.iOS forum by mistake as it requests info on Xamarin.Forms. Can this be moved to XF forum?

    Friday, November 22, 2019 6:41 PM
  • User369979 posted

    I have moved this thread to Xamarin.Forms.

    Java.Lang.SecurityException

    Message=Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{54239f6 12258:com.companyname.xpagetcontacts/u0a67} (pid=12258, uid=10067) requires android.permission.READCONTACTS or android.permission.WRITE_CONTACTS It is because we need to request the runtime permission for some significant permissions in Android 6.0+: https://developer.android.com/distribute/best-practices/develop/runtime-permissions. We could utilize the permission plugin on Forms: https://github.com/jamesmontemagno/PermissionsPlugin and change the code like:

    protected override async void OnAppearing()
    {
        base.OnAppearing();
    
        if (Device.RuntimePlatform == Device.Android)
        {
            var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Contacts);
            if (status != PermissionStatus.Granted)
            {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Contacts);
                if (results[Permission.Contacts] == PermissionStatus.Granted)
                {
                    loadContacts();
                }
            }
            else
            {
                loadContacts();
            }
        }
        else
        {
            loadContacts();
        }       
    }
    async void loadContacts()
    {
        contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync();
        lvSearchedContacts.ItemsSource = contacts;
    }
    

    Moreover, do not forget the setup steps in Android:

    // Initialize
    // Place the code below before the init of Forms
    Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
    // OnRequestPermissionsResult
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, November 26, 2019 3:09 AM
  • User42522 posted

    @LandLu Thanks for your help. It worked.

    Tuesday, November 26, 2019 5:26 PM