问候,
我正在开发一个与地址簿联系人合作的iphone应用程序。我试图在联系人中创建组,但问题是当我再次运行应用程序时,再次创建组并将新联系人保存到新创建的组中。
// create address book record
ABAddressBookRef addressBook = ABAddressBookCreate();
// create a person
ABRecordRef person = ABPersonCreate();
// name of the new person
ABRecordSetValue(person, kABPersonFirstNameProperty, [index objectAtIndex:3], nil);
ABRecordSetValue(person, kABPersonLastNameProperty, [index objectAtIndex:0], nil);
//add the new person to the record
ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, &error);
ABAddressBookAddRecord(addressBook, group, &error); // add the group
ABAddressBookSave(addressBook, &error);
ABRecordRef group = ABGroupCreate(); //create a group
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookSave(addressBook, &error);
//save the record
ABAddressBookSave(addressBook, nil);
// relase the ABRecordRef variable
CFRelease(person);
这就是我一直在努力的代码。我非常感谢任何可能的帮助
谢谢
答案 0 :(得分:1)
ABRecordRef group = ABGroupCreate();
,
它将创建一个新组...如果您想在现有组中添加成员,那么您应该按其ID获取组。 更新代码
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookInstance,putYourGroupIdHere);
谢谢,
答案 1 :(得分:0)
ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
MySource *source = [sourcesAndGroups objectAtIndex:0];
ABRecordRef group = [source.groups objectAtIndex:self.IndexValue]; //Get the Group Name
NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
ABRecordSetValue(group, kABGroupNameProperty,[self nameForGroup:group], &error);
ABAddressBookAddRecord(ab, group, &error);
ABAddressBookSave(ab, &error);
//Create new Person and save to this group
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;
isSuccess = ABRecordSetValue(record, kABPersonNicknameProperty,lastName, &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, firstName , &error);
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
if(ABMultiValueGetCount(phoneNumbers) == 0)
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Phone Number" message:@"Please enter Phone number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[av show];
return;
}
CFRelease(phoneNumbers);
NSString* phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
ABMutableMultiValueRef multi = ABRecordCopyValue(record, kABPersonEmailProperty);
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(copyOfPhones, phoneNumber,kABPersonPhoneMobileLabel,NULL);
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
isSuccess = ABRecordSetValue(record, kABPersonEmailProperty, multi, &error);
isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);
ABGroupAddMember(group, record, &error);
NSLog(@"is success %d", isSuccess);
ABAddressBookSave(ab, &error);
CFRelease(group);