gpt4 book ai didi

cocoa-touch - 以编程方式在联系人中创建一个组

转载 作者:行者123 更新时间:2023-12-02 05:36:31 25 4
gpt4 key购买 nike

如何使用 AddressBook 框架以编程方式将新组添加到 iPhone 联系人?

最佳答案

先看是否存在,如果不存在则创建:

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

关于cocoa-touch - 以编程方式在联系人中创建一个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com