contactStore.unifiedMeContactWithKeys gives me an error

Hi guys,


I am trying to access the address book contacts using contactStore.unifiedMeContactWithKeys(toFetch: keys), but it is returning me the following error:


2019-05-30 10:09:47.835832+0200 macOSContactManager[3549:185878] Could not get real path for Address Book lock folder: open() for F_GETPATH failed.

2019-05-30 10:09:47.836157+0200 macOSContactManager[3549:185878] Unable to open file lock: <ABProcessSharedLock: 0x600001778140: name=(null), lockFilePath=(null), localLock=<NSRecursiveLock: 0x600002902d10>{recursion count = 0, name = nil}, fileDescriptor=-1> Error Domain=NSPOSIXErrorDomain Code=14 "Bad address" UserInfo={ABFileDescriptor=-1}


And then this is the error from thrown

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.}


Thanks for your help

Answered by DTS Engineer in 361896022

The following code works for me:

let store = CNContactStore()
store.requestAccess(for: .contacts) { (success, _) in
    guard success else {
        print("error")
        return
    }
    do {
        let me = try store.unifiedMeContactWithKeys(toFetch: [CNContactVCardSerialization.descriptorForRequiredKeys()])
        print(me)
        // prints:
        // <CNContact: 0x100639a80: identifier=28E7CCA5-3EDF-451D-8C31-C64F4DD1FB29, givenName=Quinn, familyName=Quinn, …>
    } catch {
        print("error")
    }
}

I did a bunch of setup to get it going:

  • Import the Contact framework.

  • Turn on App Sandbox (just because) and enable Contacts.

  • Turn on Hardened Runtime and enable Address Book [1].

  • Add an

    NSContactsUsageDescription
    entry to my
    Info.plist
    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] I filed a bug (r. 51060731) about the fact this is Address Book and not Contacts (-:

Accepted Answer

The following code works for me:

let store = CNContactStore()
store.requestAccess(for: .contacts) { (success, _) in
    guard success else {
        print("error")
        return
    }
    do {
        let me = try store.unifiedMeContactWithKeys(toFetch: [CNContactVCardSerialization.descriptorForRequiredKeys()])
        print(me)
        // prints:
        // <CNContact: 0x100639a80: identifier=28E7CCA5-3EDF-451D-8C31-C64F4DD1FB29, givenName=Quinn, familyName=Quinn, …>
    } catch {
        print("error")
    }
}

I did a bunch of setup to get it going:

  • Import the Contact framework.

  • Turn on App Sandbox (just because) and enable Contacts.

  • Turn on Hardened Runtime and enable Address Book [1].

  • Add an

    NSContactsUsageDescription
    entry to my
    Info.plist
    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] I filed a bug (r. 51060731) about the fact this is Address Book and not Contacts (-:

Thanks eskimo,


My code is now working, by mistake was that I did not activated the two options below.

  • Turn on App Sandbox (just because) and enable Contacts.
  • Turn on Hardened Runtime and enable Address Book [1].


Have a nice day!

contactStore.unifiedMeContactWithKeys gives me an error
 
 
Q