How to fetch all contacts record in iOS 9 using Contacts Framework
Both other answers do only load contacts from the container with the defaultContainerIdentifier
. In a scenario, where the user has more than one container (i.e. an Exchange and an iCloud account which both are used to store contacts), this would only load the contacts from the account that is configured as the default. Therefore, it would not load all contacts as requested by the author of the question.
What you'll probably want to do instead is getting all the containers and iterate over them to extract all contacts from each of them. The following code snippet is an example of how we do it in one of our apps (in Swift):
lazy var contacts: [CNContact] = {
let contactStore = CNContactStore()
let keysToFetch = [
CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
CNContactEmailAddressesKey,
CNContactPhoneNumbersKey,
CNContactImageDataAvailableKey,
CNContactThumbnailImageDataKey]
// Get all the containers
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containersMatchingPredicate(nil)
} catch {
print("Error fetching containers")
}
var results: [CNContact] = []
// Iterate all containers and append their contacts to our results array
for container in allContainers {
let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)
do {
let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
results.appendContentsOf(containerResults)
} catch {
print("Error fetching results for container")
}
}
return results
}()
Get a list of all contacts on iOS
Perhaps ABPerson
function ABAddressBookCopyArrayOfAllPeople might do?
Example:
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
...
}
CNContact Frame work not fetching all contacts(iCloud,Gmail)
If you simply wish to get all unified contacts (Contacts in different accounts that represent the same person may be automatically linked together. Linked contacts are displayed in macOS and iOS apps as unified contacts) the best solution would be below one
-(void)fetchContacts
{
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES)
{
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey];
NSString *containerId = store.defaultContainerIdentifier;
NSArray * contactContainerArray = [store containersMatchingPredicate:nil error:nil];
CNContactFetchRequest * fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keys];
[store enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
}];
}
}
How to delete record in iPhone address book using CONTACTS Framework ios 9?
The complete AddressBook Framework was deprecated in iOS9, you should check out the Contacts Framework instead:
Contacts Framework
A nice tutorial explaining deleting records here.
The tutorial covers Swift, but the same principle applys to Objective-C as well.
Related Topics
Calayer with Transparent Hole in It
iOS 7.0 No Code Signing Identities Found
Should I Declare Variables in Interface or Using Property in Objective-C Arc
How to Add Images for Different Screen Size from Assets.Xcassets in Xcode 8
Ios: Disable Autorotation for a Subview
It's Possible to Change Push Notification Message Before Display on Device from iOS Side
Uitableview Within Uiscrollview Using Autolayout
How to Change Inactive Icon/Text Color on Tab Bar
iOS - Mkmapview Place Annotation by Using Address Instead of Lat/Long
Swift 3 Core Data Delete Object
How to Style Uitextview to Like Rounded Rect Text Field
How to Get the Font Name from an Otf or Ttf File
Add Custom Header Field in Request of Avplayer
Optional Binding with Try? and As? Still Produces an Optional Type
Switch Cameras with Avcapturesession
Generate Random Numbers with a Given Distribution