iPhone到现有组添加联系人地址簿中
我在iPhone上的应用程序,与地址簿联系人工程工作。我试图在联系人中获取make组,但问题是当我再次运行应用程序时,组又被重新创建,新联系人被保存到新创建的组中。
// 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);
这就是我一直在努力的代码。我真的很感谢所有帮助这是可能的
谢谢
ABRecordRef group = ABGroupCreate();
,
这将创建一个新的组...如果你婉在现有的组中添加成员,那么你应该通过ID获取组。 更新的代码
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookInstance,putYourGroupIdHere);
感谢,
我已经尝试了你提到的,但我得到一个错误,如“初始化使指针没有投射整数” – alanvabraham 2011-04-21 06:51:04
抱歉让你烦恼,但这是我的错误。我写错了方法,请在上面的答案中看到更新的代码。谢谢 – Ravin 2011-04-21 07:25:35
@Ravin你能帮我一个问题:[http://stackoverflow.com/questions/10333810/add-contact-to-existing-group-programatically-in-iphone][1] – iOSAppDev 2012-04-27 08:52:09
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);
哪里组变量设置? – theChrisKent 2011-04-20 20:14:07
在将记录添加到组之前,我更新了我的问题组变量。 – alanvabraham 2011-04-21 01:22:46