我正在使用以下代码,但仍无法在群组中添加联系人信息,还有一件事总是用于创建新群组。我还想检查现有的gruop是否可用!!!!
无法在群组中添加联系人!!
ABRecordRef group = ABGroupCreate(); //create a group
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); // set group's name
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookAddRecord(addressBook, group, &error); // add the group
ABAddressBookSave(addressBook, nil); //save the record
答案 0 :(得分:1)
请找到下面的工作代码......
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
CFSTR("Jijo"), &anError);
ABRecordSetValue(aRecord, kABPersonLastNameProperty,
CFSTR("Pulikkottil"), &anError);
if (anError != NULL) {
NSLog(@"error while creating..");
}
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);
ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
BOOL isAdded = ABAddressBookAddRecord (
addressBook,
aRecord,
&error
);
if(isAdded){
NSLog(@"added..");
}
if (error != NULL) {
NSLog(@"ABAddressBookAddRecord %@", error);
}
error = NULL;
BOOL isSaved = ABAddressBookSave (
addressBook,
&error
);
if(isSaved){
NSLog(@"saved..");
}
if (error != NULL) {
NSLog(@"ABAddressBookSave %@", error);
}
CFRelease(aRecord);
CFRelease(firstName);
CFRelease(lastName);
CFRelease(addressBook);
不要忘记添加AddressBook.Framework。
参考:AddressBookProgrammingGuideforiPhone.pdf。
同样讨论
http://www.iphonedevsdk.com/forum/iphone-sdk-development/12496-add-contact-address-book.html
答案 1 :(得分:0)
我使用了以下方法来完成这项任务。
<强> 1。获取组唯一ID。
ABRecordRef currentGroup = (ABRecordRef)CFBridgingRetain([source.groups objectAtIndex:groupIndex]); ABRecordID currentGroupID=ABRecordGetRecordID(currentGroup);
<强> 2。将成员添加到组。
ABRecordRef currentGroup = ABAddressBookGetGroupWithRecordID(addressBook, currentGroupID);
BOOL didAdd,didSave;
NSString *strPersonContactID=[appDelegate.arrOfSelectedContactsToEdit objectAtIndex:i];
ABRecordID personContactID=(ABRecordID)[strPersonContactID intValue];
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personContactID);
didAdd = ABGroupAddMember(currentGroup,person,&error);
if (!didAdd)
{
NSLog(@"Unresolved error while adding person group");
}
didSave = ABAddressBookSave(addressBook, &error);
if (!didSave)
{
NSLog(@"Unresolved error while saving address book");
}
CFRelease(addressBook);
如果您想检查群组是否存在,请使用群组ID,这有助于您唯一地区分群组。