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 |
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
Thank you!