C# Access denied after Contacts.CNContactStore().RequestAccess

43 Views Asked by At

I use VS for Mac version 17.4.2 I would like to read all my own MacOs contacts.

I create a new Cocoa application (Mac -> App -> Xamarin). I add the necessary parameter in info.plist :

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

I begin by requesting access, and that generates an error (see below).

In System preferences / Security/ Confidentiality, no app has asked access to Contacts.

How can I fix this issue? Thank you.

using AppKit;
using Contacts;
using Foundation;
using System;

namespace Upd2
{
    public partial class ViewController : NSViewController
    {
        private CNContactStore store;
        private CNAuthorizationStatus auth;

        public ViewController(IntPtr handle) : base(handle)
        {
            store = new Contacts.CNContactStore();
            auth = CNContactStore.GetAuthorizationStatus(CNEntityType.Contacts);
            Console.WriteLine(string.Format("\nAuthorizationStatus : {0}", auth.ToString()));
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Do any additional setup after loading the view.
            if (auth == CNAuthorizationStatus.NotDetermined)
            {
                store.RequestAccess(CNEntityType.Contacts, Handle_Contacts_RequestAccess);
            }
        }

        private void Handle_Contacts_RequestAccess(bool granted, NSError error)
        {
            Console.WriteLine(string.Format("granted : {0}", granted.ToString()));
            if (!granted)
            {
                Console.WriteLine(string.Format("Error : {0}", error.ToString()));
            }
        }

    }
}

AuthorizationStatus : NotDetermined granted : False Error : Access Denied

1

There are 1 best solutions below

0
Thorsten Dittmar On

You need to add an entry to your entitlements file as well.

<key>com.apple.security.personal-information.addressbook</key>
<true/>

This entitles your app to even ask for access to the address book in the first place.