Skip to content

Instantly share code, notes, and snippets.

@Daij-Djan
Created May 17, 2016 09:27
Show Gist options
  • Save Daij-Djan/1de5a4c690616915e99aad63af1728f2 to your computer and use it in GitHub Desktop.
Save Daij-Djan/1de5a4c690616915e99aad63af1728f2 to your computer and use it in GitHub Desktop.
//MIT
import Foundation
import Contacts
if(NSProcessInfo.processInfo().arguments.count != 2) {
print("Usage: CNPurgeGroup %GROUPNAME%")
}
else {
let groupName = NSProcessInfo.processInfo().arguments[1]
let store = CNContactStore()
let groups = try! store.groupsMatchingPredicate(nil)
for group in groups {
if(group.name == groupName) {
let groupIdentifer = group.identifier
let relevantPredicate = CNContact.predicateForContactsInGroupWithIdentifier(groupIdentifer)
let contacts = try! store.unifiedContactsMatchingPredicate(relevantPredicate, keysToFetch: [CNContactGivenNameKey])
let saveRequest = CNSaveRequest()
for contact in contacts {
saveRequest.deleteContact(contact.mutableCopy() as! CNMutableContact)
}
try! store.executeSaveRequest(saveRequest)
}
}
}
@Daij-Djan
Copy link
Author

since deleting a contact from a group in the contacts app only removes it from the group, I wrote this short snippet to 'purge' a group

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