33
44/* **** HELPERS *****/
55
6- // Parses and returns an array of email addresses as strings
6+ // Parses and returns an array of email addresses as strings.
77Napi::Array GetEmailAddresses (Napi::Env env, CNContact *cncontact) {
88 int num_email_addresses = [[cncontact emailAddresses ] count ];
99
1919 return email_addresses;
2020}
2121
22- // Parses and returns an array of phone numbers as strings
22+ // Parses and returns an array of phone numbers as strings.
2323Napi::Array GetPhoneNumbers (Napi::Env env, CNContact *cncontact) {
2424 int num_phone_numbers = [[cncontact phoneNumbers ] count ];
2525
3535 return phone_numbers;
3636}
3737
38- // Parses and returns an array of postal addresses as strings
38+ // Parses and returns an array of postal addresses as strings.
3939Napi::Array GetPostalAddresses (Napi::Env env, CNContact *cncontact) {
4040 int num_postal_addresses = [[cncontact postalAddresses ] count ];
4141 Napi::Array postal_addresses = Napi::Array::New (env, num_postal_addresses);
5252 return postal_addresses;
5353}
5454
55- // Parses and returns birthdays as strings in YYYY-MM-DD format
55+ // Parses and returns birthdays as strings in YYYY-MM-DD format.
5656std::string GetBirthday (CNContact *cncontact) {
5757 std::string result;
5858
7979 return Napi::Buffer<uint8_t >::Copy (env, &data[0 ], data.size ());
8080}
8181
82- // Creates an object containing all properties of a macOS contact
82+ // Creates an object containing all properties of a macOS contact.
8383Napi::Object CreateContact (Napi::Env env, CNContact *cncontact) {
8484 Napi::Object contact = Napi::Object::New (env);
8585
112112}
113113
114114// Parses an array of phone number strings and converts them to an NSArray of
115- // CNPhoneNumbers
115+ // CNPhoneNumbers.
116116NSArray *ParsePhoneNumbers (Napi::Array phone_number_data) {
117117 NSMutableArray *phone_numbers = [[NSMutableArray alloc ] init ];
118118
132132}
133133
134134// Parses an array of email address strings and converts them to an NSArray of
135- // NSStrings
135+ // NSStrings.
136136NSArray *ParseEmailAddresses (Napi::Array email_address_data) {
137137 NSMutableArray *email_addresses = [[NSMutableArray alloc ] init ];
138138
150150}
151151
152152// Parses a string in YYYY-MM-DD format and converts it to an NSDatComponents
153- // object
153+ // object.
154154NSDateComponents *ParseBirthday (std::string birth_day) {
155155 NSString *bday = [NSString stringWithUTF8String: birth_day.c_str ()];
156156 NSDateFormatter *formatter = [[NSDateFormatter alloc ] init ];
168168}
169169
170170// Returns a status indicating whether or not the user has authorized Contacts
171- // access
171+ // access.
172172CNAuthorizationStatus AuthStatus () {
173173 CNEntityType entityType = CNEntityTypeContacts;
174174 return [CNContactStore authorizationStatusForEntityType: entityType];
@@ -186,7 +186,7 @@ CNAuthorizationStatus AuthStatus() {
186186}
187187
188188// Returns all contacts in the CNContactStore matching a specified name string
189- // predicate
189+ // predicate.
190190NSArray *FindContacts (const std::string &name_string) {
191191 CNContactStore *addressBook = [[CNContactStore alloc ] init ];
192192
@@ -199,7 +199,7 @@ CNAuthorizationStatus AuthStatus() {
199199}
200200
201201// Creates a new CNContact in order to update, delete, or add it to the
202- // CNContactStore
202+ // CNContactStore.
203203CNMutableContact *CreateCNMutableContact (Napi::Object contact_data) {
204204 CNMutableContact *contact = [[CNMutableContact alloc ] init ];
205205
@@ -247,7 +247,7 @@ CNAuthorizationStatus AuthStatus() {
247247
248248/* **** EXPORTED FUNCTIONS *****/
249249
250- // Returns the user's Contacts access consent status as a string
250+ // Returns the user's Contacts access consent status as a string.
251251Napi::Value GetAuthStatus (const Napi::CallbackInfo &info) {
252252 Napi::Env env = info.Env ();
253253 std::string auth_status = " Not Determined" ;
@@ -264,22 +264,30 @@ CNAuthorizationStatus AuthStatus() {
264264 return Napi::Value::From (env, auth_status);
265265}
266266
267- // Returns an array of all a user's Contacts as objects
267+ // Returns an array of all a user's Contacts as objects.
268268Napi::Array GetAllContacts (const Napi::CallbackInfo &info) {
269269 Napi::Env env = info.Env ();
270270 Napi::Array contacts = Napi::Array::New (env);
271271 CNContactStore *addressBook = [[CNContactStore alloc ] init ];
272+ NSMutableArray *cncontacts = [[NSMutableArray alloc ] init ];
272273
273274 if (AuthStatus () != CNAuthorizationStatusAuthorized)
274275 return contacts;
275276
276- NSPredicate *predicate =
277- [CNContact predicateForContactsInContainerWithIdentifier:
278- addressBook.defaultContainerIdentifier];
279- NSArray *cncontacts =
280- [addressBook unifiedContactsMatchingPredicate: predicate
281- keysToFetch: GetContactKeys ()
282- error: nil ];
277+ NSArray *containers = [addressBook containersMatchingPredicate: nil error: nil ];
278+
279+ int num_containers = [containers count ];
280+ for (int idx = 0 ; idx < num_containers; idx++) {
281+ CNContainer *container = [containers objectAtIndex: idx];
282+ NSPredicate *predicate = [CNContact
283+ predicateForContactsInContainerWithIdentifier: [container identifier ]];
284+ NSArray *container_contacts =
285+ [addressBook unifiedContactsMatchingPredicate: predicate
286+ keysToFetch: GetContactKeys ()
287+ error: nil ];
288+
289+ [cncontacts addObjectsFromArray: container_contacts];
290+ }
283291
284292 int num_contacts = [cncontacts count ];
285293 for (int i = 0 ; i < num_contacts; i++) {
@@ -290,7 +298,7 @@ CNAuthorizationStatus AuthStatus() {
290298 return contacts;
291299}
292300
293- // Returns an array of all Contacts as objects matching a specified string name
301+ // Returns an array of all Contacts as objects matching a specified string name.
294302Napi::Array GetContactsByName (const Napi::CallbackInfo &info) {
295303 Napi::Env env = info.Env ();
296304 Napi::Array contacts = Napi::Array::New (env);
@@ -310,7 +318,7 @@ CNAuthorizationStatus AuthStatus() {
310318 return contacts;
311319}
312320
313- // Creates and adds a new CNContact to the CNContactStore
321+ // Creates and adds a new CNContact to the CNContactStore.
314322Napi::Boolean AddNewContact (const Napi::CallbackInfo &info) {
315323 Napi::Env env = info.Env ();
316324 CNContactStore *addressBook = [[CNContactStore alloc ] init ];
@@ -328,7 +336,7 @@ CNAuthorizationStatus AuthStatus() {
328336 return Napi::Boolean::New (env, success);
329337}
330338
331- // Removes a CNContact from the CNContactStore
339+ // Removes a CNContact from the CNContactStore.
332340Napi::Value DeleteContact (const Napi::CallbackInfo &info) {
333341 Napi::Env env = info.Env ();
334342
@@ -348,7 +356,7 @@ CNAuthorizationStatus AuthStatus() {
348356 return Napi::Boolean::New (env, success);
349357}
350358
351- // Updates an existing CNContact in the CNContactStore
359+ // Updates an existing CNContact in the CNContactStore.
352360Napi::Value UpdateContact (const Napi::CallbackInfo &info) {
353361 Napi::Env env = info.Env ();
354362
@@ -367,7 +375,7 @@ CNAuthorizationStatus AuthStatus() {
367375 return Napi::Boolean::New (env, success);
368376}
369377
370- // Initializes all functions exposed to JS
378+ // Initializes all functions exposed to JS.
371379Napi::Object Init (Napi::Env env, Napi::Object exports) {
372380 exports.Set (Napi::String::New (env, " getAuthStatus" ),
373381 Napi::Function::New (env, GetAuthStatus));
0 commit comments