这是从iPhone删除联系人的正确方法吗? [英] Is this the right way to delete a contact from the iPhone?

查看:99
本文介绍了这是从iPhone删除联系人的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我创建的iPhone中删除联系人。我试图找到一个删除联系人的好工作示例,但没有找到一个。添加联系人似乎很容易,但删除一个似乎很难。以下代码不起作用,但似乎有道理:

I am trying to delete a contact from the iPhone which I have created. I tried to find a good working example of deleting a contact, however didn't find one. Adding a contact seemed quite easy but deleting one seems hard. The following code does not work, but it seemed to be plausible:

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef delete = ABPersonCreate();

ABRecordSetValue(delete, kABPersonFirstNameProperty, @"Max", nil);
ABRecordSetValue(delete, kABPersonLastNameProperty, @"Mustermann", nil); 

ABAddressBookRemoveRecord(addressBook, delete, &error);
ABAddressBookSave(addressBook, &error);

任何人都可以帮助我。

在此先感谢您的帮助。

最大

推荐答案

问题是你正在创建一个不在地址簿内的 ABRecord 。你要做的是从 ABAddressBook 中搜索一个 ABRedord 的数组。我写了如何为你做这个:

The problem is that you're creating an ABRecord that isn't inside of the address book. What you have to do is search through an array of ABRedords from the ABAddressBook. I wrote how to do this for you:

CFErrorRef error = nil;

ABAddressBookRef addressBook = ABAddressBookCreate();
__block ABRecordRef delete = ABPersonCreate();

ABRecordSetValue(delete, kABPersonFirstNameProperty, @"Max", nil);
ABRecordSetValue(delete, kABPersonLastNameProperty, @"Mustermann", nil);

//Gets the array of everybody in the
NSArray *peopleArray = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);

//Creates a pass test block to see if the ABRecord has the same name as delete
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) {
        delete = person;
    }
    return (BOOL) pass;
};

int idx = [peopleArray indexOfObjectPassingTest:predicate];

bool removed = ABAddressBookRemoveRecord(addressBook, delete, &error);
bool saved = ABAddressBookSave(addressBook, &error);

您可以更改比较方式 ABRecord 通过更改块代码实例。它现在正在做的就是比较联系人的姓名。

You can change how you want to compare ABRecord instances by changing the block code. All it's doing now is comparing the names of the contacts.

这段代码的一个警告是它只会删除 ABRecord的一个实例 s的名称与 delete 的匹配。

A caveat with this code is that it will only delete one instance of the ABRecords whose name matches delete’s.

这篇关于这是从iPhone删除联系人的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆