[AppKit] CNPropertyNotFetchedException when using CNContactFormatter on CNContact retrieved from CNContactPicker

I am using CNContactPicker (with "CNContactKey.PostalAddresses" as "displayedKeys") to allow users to pick a contact + postal address.

Upon selection, I am retrieving the postal address using CNPostalAddressFormatter - this works without problems. However, when trying to retrieve the contact name using CNContactFormatter, this only works when the contact type is "Person". When it is "company", the app crashes with "CNPropertyNotFetchedException Reason: A property was not requested when contact was fetched" (because the formatter attempts to access "organizationName", which it apparently can't.

There also doesn't seem to be a way to "retrieve" this information without having to request contacts permissions manually. The same code (just using CNContactPickerViewController, but also without requesting contacts permissions) works without issue on iOS.

We need the following information in order to better assist you:

  • Code reproducing your issue.
  • iOS version reproducing your issue.
  • Does the issue also occur when using Xcode 16 beta 2?

Sorry for the late response, for some reason I wasn't notified via mail about your response. Here's there information you were asking for:

Code reproducing your issue:

#import "ViewController.h"
@import Contacts;
@import ContactsUI;

@implementation ViewController

CNContactPicker *contactPicker;

-(IBAction)buttonPressed:(id)sender {
    
    contactPicker = [[CNContactPicker alloc] init];
    contactPicker.delegate = self;
    contactPicker.displayedKeys = @[CNContactPostalAddressesKey];
    [contactPicker showRelativeToRect:NSZeroRect ofView:sender preferredEdge:NSRectEdgeMinY];
}

- (void)contactPicker:(CNContactViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
    
    CNPostalAddressFormatter *formatter = [[CNPostalAddressFormatter alloc] init];
    NSString *formattedAddress = [formatter stringFromPostalAddress:contactProperty.value];
    NSLog(@"Formatted Address:\n%@", formattedAddress);

    @try
    {
        CNContactFormatter *contactFormatter = [[CNContactFormatter alloc] init];
        NSString *formattedContact = [contactFormatter stringFromContact:contactProperty.contact];
        NSLog(@"Formatted Contact:\n%@", formattedContact);
    }
    @catch(id anException)
    {
        NSLog(@"Exception:\n%@", anException);
    }
}

@end

iOS version reproducing your issue:

This is not an iOS issue. It happens in AppKit on macOS Sonoma 14.6.1.

Does the issue also occur when using Xcode 16 beta 2?

I didn't test Xcode 16 Beta 2 but it also happens with the (more recent) Xcode 16.1 Beta

[AppKit] CNPropertyNotFetchedException when using CNContactFormatter on CNContact retrieved from CNContactPicker
 
 
Q