iOS的通讯录:字符串中的联系人姓名 [英] Address Book iOS : Contact name from string

查看:131
本文介绍了iOS的通讯录:字符串中的联系人姓名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的应用程序将联系人自动添加到iOS通讯簿,其中联系人的名称来自NSString。我试图弄清楚(请参见下面的代码),但是没有用。它可以使用我提供的第一个代码添加联系人(我有保存代码和东西),但是我想从可能有所不同的字符串中添加联系人,而不仅仅是一个不能添加的名称。

I am trying to automatically add contact to the iOS address book from my app, where the name of the contact is from a NSString. I have tried to figure it out (see code under), but It didn't work. It works to add contacts with the first code I have provided (I have the save code and stuff), but I would like to add contact from string that may vary, not just a name that can't.

ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty,@"Davis11", &error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Scott", &error);

我也尝试过此代码,但是没有运气:

I have also tried this code, with no luck:

ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty,fName, &error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, lName, &error);


推荐答案

您在做什么很好,但是您已经忘记了调用 ABAddressBookAddRecord ABAddressBookSave 。除非您这样做,否则什么都不会发生。您所拥有的就是一个随处可见的人。如果您想将该人成为通讯录的一部分,则必须将该人放入

What you are doing is fine, but you have forgotten to call ABAddressBookAddRecord and ABAddressBookSave. Nothing will happen until you do that. What you've got is a person floating around loose. You have to put that person into the address book if you want it to be part of the address book.

另外,请记住要进行内存管理。这是一个完整的示例(但是示例中省略了错误检查!在现实生活中不要这样做):

Also, please remember to do memory management. Here's a complete example (but error checking is omitted from the example! do not do that in real life):

CFErrorRef err = nil;
ABAddressBookRef adbk = ABAddressBookCreateWithOptions(nil, &err);
ABRecordRef snidely = ABPersonCreate();
ABRecordSetValue(snidely, kABPersonFirstNameProperty, @"Snidely", nil);
ABRecordSetValue(snidely, kABPersonLastNameProperty, @"Whiplash", nil);
ABAddressBookAddRecord(adbk, snidely, nil);
ABAddressBookSave(adbk, nil);
if (snidely) CFRelease(snidely);
if (adbk) CFRelease(adbk);

这篇关于iOS的通讯录:字符串中的联系人姓名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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