swiftmacoscontacts-framework

CNContactStore execute fails


I am working out how to use the Contacts framework, however some fairly simple code to create a contact is failing with an unexpected result. This is my code:

    let Store = CNContactStore()
    Store.requestAccess(for: .contacts, completionHandler:{ success, error in
        if success {
            let Contact = CNMutableContact()
            Contact.givenName = "Dave"
            Contact.familyName = "Nottage"
            let SaveRequest = CNSaveRequest()
            SaveRequest.add(Contact, toContainerWithIdentifier: nil)
            do {
                try Store.execute(SaveRequest)
                print("Success")
            }
            catch let error as NSError {
                print(error.localizedDescription)
            }
        } else {
            print("No access")
        }
    })

..and this is the result:

2019-02-22 10:30:56.050344+1030 ContactsTest[30329:25254955] [default] Unable to load Info.plist exceptions (eGPUOverrides)
2019-02-22 10:30:57.973724+1030 ContactsTest[30329:25254955] Could not get real path for Address Book lock folder: open() for F_GETPATH failed.
2019-02-22 10:30:57.973954+1030 ContactsTest[30329:25254955] Unable to open file lock: <ABProcessSharedLock: 0x600001752ac0: name=(null), lockFilePath=(null), localLock=<NSRecursiveLock: 0x600002914a80>{recursion count = 0, name = nil}, fileDescriptor=-1> Error Domain=NSPOSIXErrorDomain Code=14 "Bad address" UserInfo={ABFileDescriptor=-1}
The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)

Any ideas on what might be causing this?

Edit: Note also that this is being compiled for macOS 10.14 SDK, and is running on macOS 10.14.3


Solution

  • The answer is to check the Contacts checkbox of App Data in the App Sandbox section in Capabilities and turn on the switch for App Sandbox.