Last active
March 6, 2023 20:05
-
-
Save willthink/024f1394474e70904728 to your computer and use it in GitHub Desktop.
Using CNContactStore in Objective C to query contacts info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Hi macgusto88,
- 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
Thank you!
Excelent Code, thanks very mouch
Thanks, excellent.
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?
Thanks!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?