@@ -148,6 +148,46 @@ -(void) getAllContacts:(RCTResponseSenderBlock) callback
148148 [self retrieveContactsFromAddressBook: contactStore withThumbnails: withThumbnails withCallback: callback];
149149}
150150
151+ -(void ) getAllContactsCount : (RCTResponseSenderBlock) callback
152+ {
153+ CNContactStore* contactStore = [self contactsStore: callback];
154+ if (!contactStore)
155+ return ;
156+
157+ NSMutableArray *contacts = [[NSMutableArray alloc ] init ];
158+
159+ NSError * contactError;
160+ [contactStore containersMatchingPredicate: [CNContainer predicateForContainersWithIdentifiers: @[contactStore.defaultContainerIdentifier]] error: &contactError];
161+
162+
163+ NSMutableArray *keysToFetch = [[NSMutableArray alloc ]init];
164+ [keysToFetch addObjectsFromArray: @[
165+ CNContactEmailAddressesKey,
166+ CNContactPhoneNumbersKey,
167+ CNContactFamilyNameKey,
168+ CNContactGivenNameKey,
169+ CNContactMiddleNameKey,
170+ CNContactPostalAddressesKey,
171+ CNContactOrganizationNameKey,
172+ CNContactJobTitleKey,
173+ CNContactImageDataAvailableKey,
174+ CNContactUrlAddressesKey,
175+ CNContactBirthdayKey
176+ ]];
177+
178+ CNContactFetchRequest * request = [[CNContactFetchRequest alloc ]initWithKeysToFetch:keysToFetch];
179+ BOOL success = [contactStore enumerateContactsWithFetchRequest: request error: &contactError usingBlock: ^(CNContact * __nonnull contact, BOOL * __nonnull stop){
180+ NSDictionary *contactDict = [self contactToDictionary: contact withThumbnails: false ];
181+ [contacts addObject: contactDict];
182+ }];
183+
184+ int contactsCount = [contacts count ];
185+
186+ NSNumber *count = [NSNumber numberWithInt: contactsCount];
187+
188+ callback (@[count]);
189+ }
190+
151191RCT_EXPORT_METHOD (getAll:(RCTResponseSenderBlock) callback)
152192{
153193 [self getAllContacts: callback withThumbnails: true ];
@@ -158,6 +198,11 @@ -(void) getAllContacts:(RCTResponseSenderBlock) callback
158198 [self getAllContacts: callback withThumbnails: false ];
159199}
160200
201+ RCT_EXPORT_METHOD (getCount:(RCTResponseSenderBlock) callback)
202+ {
203+ [self getAllContactsCount: callback];
204+ }
205+
161206-(void ) retrieveContactsFromAddressBook : (CNContactStore*)contactStore
162207 withThumbnails : (BOOL ) withThumbnails
163208 withCallback : (RCTResponseSenderBlock) callback
0 commit comments