如何使用ABAddressBook删除iPhone通讯录中的记录?

发布于 2024-10-03 18:28:26 字数 225 浏览 7 评论 0原文

我正在学习地址簿框架,然后我想从iPhone通讯录中删除记录。我检查了文档,发现了一个名为 ABAddressBookRemoveRecord 的函数,但我找不到删除记录的方法,就像用户选择一条记录,然后单击删除按钮,然后单击该记录一样将会被删除。

到目前为止我所做的就是把头撞在文档上,仅此而已。

您能给我提供一个链接或示例如何删除地址簿中的记录吗?

谢谢你!

I was learning the addressbook framework and then I wanted to delete the record from the the iPhone contact book. I checked up the documentation and found a function called ABAddressBookRemoveRecord, but I can't find out a way to delete the records, like the user will select a record and then hit the delete button and then the record will get deleted.

All I did till now is banged my head over the documentation and that's all.

Can you please provide me a link or an example how to delete a record in the address book?

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪冰清 2024-10-10 18:28:26

查看 ABPersonViewController+Delete 类别,该类别无需使用任何私有方法即可删除联系人:

https://github.com/shrtlist/ AB删除

Check out the ABPersonViewController+Delete category which enables contact deletion without using any private methods:

https://github.com/shrtlist/ABDelete

壹場煙雨 2024-10-10 18:28:26

当您通过ABAddressBookRemoveRecord删除记录时,您应该通过ABAddressBookSave保存最终结果。如果你想要一个UIInterface来删除记录,我认为你需要自己实现。苹果提供的联系人UI位于ABAddressBookUI框架内。

When you delete a record by ABAddressBookRemoveRecord, you should save the final result by ABAddressBookSave. If you want a UIInterface to delete the record, I think you need to implement by yourself. The UIs about contacts provided by apple are inside the ABAddressBookUI framework.

剧终人散尽 2024-10-10 18:28:26

Objective C 代码:

ABAddressBookRef addressBook; 
CFErrorRef error = NULL; 
addressBook = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,<YOUR 'PERSON' GOES HERE>);
ABAddressBookRemoveRecord(addressBook, (ABRecordRef)person, &error );
if(error !=NULL)
{
    // Handle success

}

ABAddressBookSave(addressBook, NULL);

Swift 代码:

var emptyDictionary: CFDictionaryRef?
        var addressBookRef: ABAddressBookRef?
        var err: Unmanaged<CFErrorRef>? = nil
        var userRecord: ABRecordRef?
        addressBookRef = ABAddressBookCreateWithOptions(emptyDictionary, &err)?.takeRetainedValue()
        userRecord = ABAddressBookGetPersonWithRecordID(addressBookRef, "Record ID of User").takeUnretainedValue()

        ABAddressBookRemoveRecord(addressBookRef, userRecord, &err)
        if err != nil {
            // Handle success
        }

        // Save Address Book changes
        if ABAddressBookHasUnsavedChanges(addressBookRef){
            var err: Unmanaged<CFErrorRef>? = nil
            let savedToAddressBook = ABAddressBookSave(addressBookRef, &err)
            if savedToAddressBook {
                print("Successully saved changes.")
            } else {
                print("Couldn't save changes.")
            }
        } else {
            print("No changes occurred.")
        }

Objective C code:

ABAddressBookRef addressBook; 
CFErrorRef error = NULL; 
addressBook = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,<YOUR 'PERSON' GOES HERE>);
ABAddressBookRemoveRecord(addressBook, (ABRecordRef)person, &error );
if(error !=NULL)
{
    // Handle success

}

ABAddressBookSave(addressBook, NULL);

Swift Code:

var emptyDictionary: CFDictionaryRef?
        var addressBookRef: ABAddressBookRef?
        var err: Unmanaged<CFErrorRef>? = nil
        var userRecord: ABRecordRef?
        addressBookRef = ABAddressBookCreateWithOptions(emptyDictionary, &err)?.takeRetainedValue()
        userRecord = ABAddressBookGetPersonWithRecordID(addressBookRef, "Record ID of User").takeUnretainedValue()

        ABAddressBookRemoveRecord(addressBookRef, userRecord, &err)
        if err != nil {
            // Handle success
        }

        // Save Address Book changes
        if ABAddressBookHasUnsavedChanges(addressBookRef){
            var err: Unmanaged<CFErrorRef>? = nil
            let savedToAddressBook = ABAddressBookSave(addressBookRef, &err)
            if savedToAddressBook {
                print("Successully saved changes.")
            } else {
                print("Couldn't save changes.")
            }
        } else {
            print("No changes occurred.")
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文