如何以编程方式修改通讯录中的联系电话? [英] How to modify a contact number in address book programmatically?

查看:81
本文介绍了如何以编程方式修改通讯录中的联系电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在编写一个应用程序,该应用程序应使用户能够修改应用程序中的联系方式(主要是电话号码),然后这些修改应直接反映到通讯簿中。

I am currently writing an app which should enable the user to modify the contact details (mainly numbers) in the app, and then these modifications should be reflected directly to the Address Book.

我在Internet上进行了彻底的搜索,但是发现的所有示例都是加载联系人或添加新联系人,但对修改现有联系人没有任何帮助。

I searched thoroughly on the internet, but all the examples I found were either to load the contacts or add new contact, but nothing on modifying an existing contact.

如果他已经存储了多个电话号码,我该如何获取单个联系人的所有电话号码的列表。

also how can I get a list of all phone numbers of a single contact, in case he has several numbers stored.

推荐答案

要允许用户直接编辑其详细信息,请参阅 Apple关于显示和编辑个人记录的文档。开头部分说:设置委托,必须采用ABPersonViewControllerDelegate协议。要允许用户编辑记录,请将allowEditing设置为YES。

To allow a user to edit their details directly, see Apple's documentation on displaying and editing a person record. The initial section of that says, "Set the delegate, which must adopt the ABPersonViewControllerDelegate protocol. To allow the user to edit the record, set allowsEditing to YES."

示例:

ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
personViewController.personViewDelegate = self;
personViewController.allowsEditing = YES;

除了设置allowEditing之外,代码与显示个人详细信息所需的代码完全相同,而无需设置编辑。 (此代码摘自此答案,其中显示了更完整的内容例如,从通讯录中删除联系人。)

Other than setting allowsEditing, the code would be exactly the same as that required to display a person's details without editing. (This code was drawn from this answer which displays a fuller example regarding deleting a contact from the address book.)

不过,我看到您的标题是指以编程方式进行此操作。苹果的 iOS的通讯簿编程指南说请记住,通讯录数据库最终归用户所有,因此应用程序必须小心,不要对其进行意外更改。通常,更改应由用户启动或确认。

However, I see your title refers to doing so programatically. Apple's Address Book Programing Guide for iOS says, "Remember that the Address Book database is ultimately owned by the user, so applications must be careful not to make unexpected changes to it. Generally, changes should be initiated or confirmed by the user."

但是,有可能。以下示例出现在该文档的第17页上:

However, it is possible. The following example appears on p.17 of that document:

ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
bool didSet;

didSet = ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Katie"), &anError);
if (!didSet) {/* Handle error here. */}

didSet = ABRecordSetValue(aRecord, kABPersonLastNameProperty, CFSTR("Bell"), &anError);     
if (!didSet) {/* Handle error here. */}

CFStringRef firstName, lastName; 
firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);
/* ... Do something with firstName and lastName. ... */

CFRelease(aRecord);
CFRelease(firstName);
CFRelease(lastName);

这篇关于如何以编程方式修改通讯录中的联系电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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