1 Reply
      Latest reply on Oct 14, 2019 8:04 PM by Smithers
      KeithB Level 1 Level 1 (15 points)



        I'm trying to update my old AddressBook-based code with the Contacts framework. However, the Contacts framework seems far less reliable. In particular, I am trying to get the user's "My Card" information. In AddressBook, this is straightforward. I just need to do this:


        ABPerson *person = [[ABAddressBook sharedAddressBook] me];


        The equivalent for the Contacts framework would seem to be:


        CNContactStore *contactStore = [[CNContactStore alloc] init];
        CNContact *contact = [contactStore unifiedMeContactWithKeysToFetch:@[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPostalAddressesKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey] error:&error];


        However, this method returns nil if the user's "My Card" in Contacts is in the "iCloud" list rather than in the "All on My Mac" list (and returns an error of "Error Domain=CNErrorDomain Code=200 "Updated Record Does Not Exist" UserInfo={NSLocalizedDescription=Updated Record Does Not Exist, NSLocalizedFailureReason=The save request failed because it updates a record that does not exist or has already been deleted.}").


        If I create a card in the "All on My Mac" section and set it to "My Card" using Card > Make This My Card" in Contacts app, -unifiedMeContactWithKeysToFetch: works. But if "My Card" is only listed in the "All Contacts" and "All iCloud" sections and not in "All on My Mac", then it fails and returns nil.


        By contrast, [[ABAddressBook sharedAddressBook] me] works regardless of where "My Card" is stored. This means that, for now, I have to keep on using the older AddressBook framework and cannot upgrade to the Contacts framework as it doesn't seem to give me the information I need. Am I missing some way of getting the "My Card" no matter where it is in Contacts, or is this a bug in the current Contacts framework?