ABAddressBook如何设置返回的ABRecordRef组?

时间:2012-05-10 06:25:30

标签: ios xcode abaddressbook

所以我称之为这个方法:

- (void)newPersonViewController:(ABNewPersonViewController*)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person

返回ABRecordRef。但我无法使用ABGroupAddMember看到它。

如果我手动创建ABRecordRef,我可以设置组,但不能使用委托方法中返回的 person 对象。

例如,此代码有效(借用此处的帖子,找不到链接):

CFErrorRef error = NULL;

// 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); 

ABAddressBookSave(addressBook, &error);

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

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);  

如何使该代码与返回的Person ABRecordRef对象一起使用?

感谢。

1 个答案:

答案 0 :(得分:0)

好的,所以修好了(有时问问题会让你解决问题)

int i = ABRecordGetRecordID(person);
ABRecordRef p = ABAddressBookGetPersonWithRecordID(addressBook,i);

正确创建新的ABRecordRef似乎可以解决问题。不知道为什么。也许有人可以说你为什么需要重铸它?