问题是您正在创建一个ABRecord
不在通讯簿中的地址。ABRedord
您需要做的是从 .s 中搜索 s数组ABAddressBook
。我写了如何为你做到这一点:
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreate();
__block ABRecordRef toDelete = ABPersonCreate();
ABRecordSetValue(toDelete, kABPersonFirstNameProperty, @"Max", nil);
ABRecordSetValue(toDelete, kABPersonLastNameProperty, @"Mustermann", nil);
// Gets the array of everybody in the address book
NSArray *peopleArray = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
// Creates a test predicate to see if the ABRecord has the same name as toDelete
BOOL (^predicate)(id obj, NSUInteger idx, BOOL *stop) = ^(id obj, NSUInteger idx, BOOL *stop) {
ABRecordRef person = (__bridge ABRecordRef)obj;
CFComparisonResult result = ABPersonComparePeopleByName(person, delete, kABPersonSortByLastName);
bool pass = (result == kCFCompareEqualTo);
if (pass) {
toDelete = person;
}
return (BOOL) pass;
};
int idx = [peopleArray indexOfObjectPassingTest:predicate];
bool removed = ABAddressBookRemoveRecord(addressBook, toDelete, &error);
bool saved = ABAddressBookSave(addressBook, &error);
您可以ABRecord
通过更改块代码来更改比较实例的方式。它现在所做的只是比较联系人的姓名。
这段代码需要注意的是,它只会删除一个ABRecord
名称与delete
's 匹配的 s 实例。