在iPhone的地址簿中创建一个组

前端之家收集整理的这篇文章主要介绍了在iPhone的地址簿中创建一个组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用iPhone应用程序,该应用程序使用地址簿添加联系人.我已经能够将联系人添加到地址簿中,但我遇到的问题是在我创建的组中添加联系人记录时.

联系是在不在组中已创建的所有联系人下创建的.以下是我使用过的代码

// create address book record
ABAddressBookRef addressBook = ABAddressBookCreate(); 
// create a person  
ABRecordRef person = ABPersonCreate();  
// first name of the new person
ABRecordSetValue(person,kABPersonFirstNameProperty,@"FirstName",nil);
// his last name 
ABRecordSetValue(person,kABPersonLastNameProperty,@"LastName",nil);  
//add the new person to the record
ABAddressBookAddRecord(addressBook,person,nil); 

ABRecordRef group = ABGroupCreate(); //create a group 
ABRecordSetValue(group,kABGroupNameProperty,@"My Group",&error); // set group's name 
ABGroupAddMember(group,&error); // add the person to the group         
ABAddressBookAddRecord(addressBook,group,&error); // add the group   

//save the record
ABAddressBookSave(addressBook,nil);  

// relase the ABRecordRef  variable
CFRelease(person);

解决方法

首先需要将Person保存到地址簿,然后再将其添加到组中,这意味着您必须添加一个

ABAddressBookSave(addressBook,nil);

在将此人添加到组之前,在您的情况下,它将在创建组之前.

猜你在找的Xcode相关文章