Skip to content

Instantly share code, notes, and snippets.

@jdg
Created December 18, 2015 06:36
Show Gist options
  • Save jdg/70a2c5bbd4d9abc0bfc7 to your computer and use it in GitHub Desktop.
Save jdg/70a2c5bbd4d9abc0bfc7 to your computer and use it in GitHub Desktop.
Delete all iOS contacts using the Contacts.framework
- (void) deleteAllContacts {
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
NSArray *keys = @[CNContactPhoneNumbersKey];
NSString *containerId = contactStore.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [contactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
for (CNContact *contact in cnContacts) {
[saveRequest deleteContact:[contact mutableCopy]];
}
[contactStore executeSaveRequest:saveRequest error:nil];
DDLogVerbose(@"Deleted contacts %lu", cnContacts.count);
}
}
}];
}
@Inderpal610
Copy link

Hello, Can i know how to deleted selected contacts.

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