以编程方式在联系人中创建组 [英] Programmatically create a group in contacts
本文介绍了以编程方式在联系人中创建组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式使用AddressBook框架将新群组添加到iPhone联系人?
How to programmatically add a new group to the iPhone contact using AddressBook framework?
推荐答案
首先看看是否存在,如果没有,创建它:
First look and see if it exists, and if not, create it:
bool foundIt = NO;
// Protective - did we just not find it, or lose it?
CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addrBook);
CFIndex numGroups = CFArrayGetCount(groups);
for(CFIndex idx=0; idx<numGroups; ++idx) {
ABRecordRef groupItem = CFArrayGetValueAtIndex(groups, idx);
CFStringRef name = (CFStringRef)ABRecordCopyValue(groupItem, kABGroupNameProperty);
//NSLog(@"Look at group named %@", name);
bool isMatch = [newName isEqualToString:(NSString *)name];
CFRelease(name);
if(isMatch) {
// NSLog(@"FOUND THE GROUP ALREADY!");
groupNum = [NSNumber numberWithInt:ABRecordGetRecordID(groupItem)];
[self setObject:groupNum forKey:kGroupID];
foundIt = YES;
break;
}
}
CFRelease(groups);
if(!foundIt) {
// lets create one
ABRecordRef groupItem = ABGroupCreate();
ABRecordSetValue(groupItem, kABGroupNameProperty, (CFStringRef *)newName, &error);
if(!error) {
ABAddressBookAddRecord (addrBook, groupItem, &error); // bool ret =
ABAddressBookSave(addrBook, &error);
groupNum = [NSNumber numberWithInt:ABRecordGetRecordID(groupItem)];
//NSLog(@"FIRST groupNumber: %@", groupNum);
[self setObject:groupNum forKey:kGroupID];
}
CFRelease(groupItem);
}
这篇关于以编程方式在联系人中创建组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文