【问题标题】:Create a group in address book for iPhone在 iPhone 通讯录中创建群组
【发布时间】:2018-11-21 06:45:47
【问题描述】:

我正在使用将联系人添加到地址簿的 iPhone 应用程序。我已经能够将联系人添加到通讯簿,但我面临的问题是在将联系人记录添加到我创建的组时。

联系人是在已创建的组以外的所有联系人下创建的。下面是我用过的代码

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

【问题讨论】:

  • 你的意思是你不能将人添加到组对吗?
  • 是的,此人未添加到组中,但已添加到所有联系人中。

标签: iphone objective-c xcode


【解决方案1】:

这是我的测试,我测试它,效果很好。

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

【讨论】:

  • 上述代码的问题是会创建多个名为“我的组”的组。它不会将内容添加到现有组,这是原始问题所要求的。我被困在哪里:-)
【解决方案2】:

您首先需要先将 Person 保存到通讯录中,然后再将其添加到组中,这意味着您必须添加一个

ABAddressBookSave(addressBook, nil);

在将人添加到组之前,在您的情况下,它将在创建组之前。

【讨论】:

  • 我已经按照你提到的那样尝试了,但仍然无法正常工作,联系人保存在所有联系人中,而不是在组内
  • 至于通讯录中的人,加群后也需要保存,加群前,一切都需要保存在通讯录中。所以创造人,保存。创建组,保存。将人添加到组中,保存。
  • 我试过它喜欢它仍然没有工作。你知道在通讯录中添加联系人的其他方法吗
  • 这是我知道的唯一方法,但这很奇怪,因为我已经尝试过我所说的,并且它有效,Xcode 4 + iOS SDK 4.3
  • 我尝试过的示例链接,告诉我它是否适合您:cl.ly/67s8
猜你喜欢
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
相关资源
最近更新 更多