0

所以我调用这个方法:

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

它返回一个 ABRecordRef。但是我看不到使用 ABGroupAddMember 的组。

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

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

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 对象一起使用?

谢谢。

4

1 回答 1

0

好的,所以修复了它(有时问这个问题会让你头脑清醒以解决它)

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

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

于 2012-05-10T06:54:38.230 回答