Address Book iOS : Contact name from string

只愿长相守 提交于 2019-12-12 00:47:48

问题


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);

回答1:


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);


来源:https://stackoverflow.com/questions/22332545/address-book-ios-contact-name-from-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!