I hace seen example code for using the CNContact framework with iOS but not macOS. Does anyone have any links to example projects?
macos CNContactPicker Example Code
CNContact is both for IOS, OSX and WathcOS.
What is your problem with the IOS example ? Is it that hard to adpat to OSX ?
I am having several problems, so was just hoping to see a working project. For example:
trying yo access the Contacts within Groups in my mac’s Contacts.
My code is like this:
```let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey]
let groups = try contactStore.groups(matching: nil)
for group in groups {
let groupPredicate = CNContact.predicateForContactsInGroup(withIdentifier: group.name)
let contacts = try contactStore.unifiedContacts(matching: groupPredicate, keysToFetch: keysToFetch as [CNKeyDescriptor])```
When I run this, it finds the groups correctly, but contacts is empty for each group. Several seconds later I see the following message in the console:
*2018-03-11 14:32:00.049001+0000 ContactMess[3763:617682] [Accounts] Failed to update account with identifier 78C338E9-BFAF-431E-AC68-9115BAAAC162, error: Error Domain=ABAddressBookErrorDomain Code=1002 "(null)”*
Could also have a look at WWDC
h ttps://developer.apple.com/videos/play/wwdc2015/223/
They explain first that API is the same for IOS and OSX now on. So, IOS example should really be easy to port to MacOS
Yes thanks for that. I think the code is very similar and should port across ok. However, in doing so, I get the error message I showed earlier ([Accounts] Failed to update account with identifier 78C338E9-BFAF-431E-AC68-9115BAAAC162, error: Error Domain=ABAddressBookErrorDomain Code=1002 "(null)" ) I don't know if this is a problem with my code (probably) or what? So I would just like to run some supposedly working macOS code for this to make sure it will work with my setup.
The earlier post you mention was never published it seems.
Did you define a string for NSContactsUsageDescription in info.plist to allow access to the contacts ?
See h ttps://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
NSContactsUsageDescription
NSContactsUsageDescription (String - iOS) The key lets you describe the reason your app accesses the user’s contacts. When the system prompts the user to allow access, this string is displayed as part of the alert.Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and that accesses the user’s contacts, must statically declare the intent to do so. Include the NSContactsUsageDescription key in your app’s
Info.plist file and provide a purpose string for this key. If your app attempts to access the user’s contacts without a corresponding purpose string, your app exits.This key is supported in iOS 6.0 and later.