-
-
Save sssbohdan/b350c0edd03117125c5d5200c3e2ff75 to your computer and use it in GitHub Desktop.
Using CNContactStore in Objective C to query contacts info
This file contains 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"]; | |
NSStrubg * 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment