Created
December 18, 2015 06:36
-
-
Save jdg/70a2c5bbd4d9abc0bfc7 to your computer and use it in GitHub Desktop.
Delete all iOS contacts using the Contacts.framework
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
- (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); | |
} | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Can i know how to deleted selected contacts.