Skip to content

Instantly share code, notes, and snippets.

@willthink
Last active March 6, 2023 20:05
Show Gist options
  • Save willthink/024f1394474e70904728 to your computer and use it in GitHub Desktop.
Save willthink/024f1394474e70904728 to your computer and use it in GitHub Desktop.
Using CNContactStore in Objective C to query contacts info
#import <Contacts/Contacts.h>
@implementation ContactsScan
- (void) contactScan
{
if ([CNContactStore class]) {
//ios9 or later
CNEntityType entityType = CNEntityTypeContacts;
if( [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusNotDetermined)
{
CNContactStore * contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
[self getAllContact];
}
}];
}
else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized)
{
[self getAllContact];
}
}
}
-(void)getAllContact
{
if([CNContactStore class])
{
//iOS 9 or later
NSError* contactError;
CNContactStore* addressBook = [[CNContactStore alloc]init];
[addressBook containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[addressBook.defaultContainerIdentifier]] error:&contactError];
NSArray * keysToFetch =@[CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey];
CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
BOOL success = [addressBook enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){
[self parseContactWithContact:contact];
}];
}
}
- (void)parseContactWithContact :(CNContact* )contact
{
NSString * firstName = contact.givenName;
NSString * lastName = contact.familyName;
NSString * phone = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"];
NSString * email = [contact.emailAddresses valueForKey:@"value"];
NSArray * addrArr = [self parseAddressWithContac:contact];
}
- (NSMutableArray *)parseAddressWithContac: (CNContact *)contact
{
NSMutableArray * addrArr = [[NSMutableArray alloc]init];
CNPostalAddressFormatter * formatter = [[CNPostalAddressFormatter alloc]init];
NSArray * addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"];
if (addresses.count > 0) {
for (CNPostalAddress* address in addresses) {
[addrArr addObject:[formatter stringFromPostalAddress:address]];
}
}
return addrArr;
}
@end
@startingseb
Copy link

brilliant! Thanks so much 👍

@mcgusto88
Copy link

I'm am trying to use you code to request permission to access contacts but the code for asking for permission doesn't get executed. Also, can the address book code that is deprecated be used for same purpose? Lastly, do you have any resources for iOS 9 documentation for objective c?

@willthink
Copy link
Author

Hi macgusto88,

  1. Can you make sure the device you are running on haven't determined contact permission for your App? Since the line 13 which try to access the permission will only execute if
 [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusNotDetermined) 

is true
2. The deprecated API still work now, but at some point apple may remove it. This stackoverflow explain it
3. If you're referring CNContacStore framework, or other frameworks, Apple developer website would be the best place

@Mefteg
Copy link

Mefteg commented Jan 27, 2017

Thank you!

@risalgue
Copy link

Excelent Code, thanks very mouch

@AvcuFurkan
Copy link

Thanks, excellent.

@Ilesh
Copy link

Ilesh commented Nov 30, 2017

Hello willthink, Can I find the container source and its identifier like I want to fetch only google contacts from address book with its google id. is it possible?

@rahul128L
Copy link

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment