开发者

Create a group in address book for iPhone

I am working in an iPhone application which uses adding contact to the address book. I have been able to add contacts to the address book but the problem I am facing is while adding a contact record to a group that I have created.

The contact is created under all contacts not within the group which have been created. Below is the code I have used

// 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, person, &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);  


This is My test , I test it , it works well.

ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"new 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,@"GroupMember nick name", &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, @"Middle name", &error);

ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);

CFTypeRef phone= CFSTR("123000222111");

ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);

isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);

isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);

ABGroupAddMember(group, record, &error);

NSLog(@"is success %d", isSuccess);

ABAddressBookSave(ab, &error);
CFRelease(group);  


You first need to save the Person to the address book before adding it to the group, this means you have to add an

ABAddressBookSave(addressBook, nil);

Before adding the person to the group, in your case it will be just before creating the group.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜