ios - 使用联系人框架将联系人添加到群组

标签 ios cncontactstore

我使用联系人组创建了一个组,然后我想向该组添加一个联系人。

        NSPredicate *predicate = [CNGroup predicateForGroupsWithIdentifiers:@[[[NSUserDefaults standardUserDefaults]objectForKey:@"groupIDentifier"]]];
        NSArray *groups = [store groupsMatchingPredicate:predicate error:&saveError];

        CNMutableContact *contact = [[CNMutableContact alloc] init];
        contact.familyName = @"Doe";
        contact.givenName = @"John";

        CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"312-555-1212"]];
        contact.phoneNumbers = @[homePhone];

        CNSaveRequest *request = [CNSaveRequest new];
        CNGroup *group = [groups firstObject];
        [request addContact:contact toContainerWithIdentifier:group.identifier];

        if (![store executeSaveRequest:request error:&saveError]) {
            NSLog(@"error = %@", saveError);
        }

错误是:

error = Error Domain=CNErrorDomain Code=200 "Updated Record Does Not Exist" UserInfo={CNInvalidRecordIdentifiers=( "45FFBB0D-C74B-4A14-8293-9099EA7DEF81:ABGroup" ), NSLocalizedDescription=Updated Record Does Not Exist, NSLocalizedFailureReason=The save request failed because it updates a record that does not exist or has already been deleted.}

我也试过使用:

[request addMember:contact toGroup:[groups firstObject]];

在这种情况下,错误是:

error = Error Domain=CNErrorDomain Code=200 "Updated Record Does Not Exist" UserInfo={CNInvalidRecords=(
    "<CNContact: 0x7f8ce97aa640: identifier=7CC6BC1D-1B23-48DA-8282-06115F542A97:ABPerson, givenName=John, familyName=Doe, organizationName=, phoneNumbers=(\n    \"<CNLabeledValue: 0x600001873cc0: identifier=68277209-3AE4-40AF-9EEA-DF0E1D01883C, label=_$!<Home>!$_, value=<CNPhoneNumber: 0x600000433300: stringValue=312-555-1212, initialCountryCode=(null)>>\"\n), emailAddresses=(\n), postalAddresses=(\n)>" ), NSLocalizedFailureReason=The save request failed because it updates a record that does not exist or has already been deleted., NSLocalizedDescription=Updated Record Does Not Exist}

最佳答案

我发现的疯狂之处在于:我需要同时调用 addMemberaddContact,才能真正将联系人添加到组中。

    CNGroup *group = [groups firstObject];
    [request addMember:contact toGroup:group];
    [request addContact:contact toContainerWithIdentifier:nil];

if (![store executeSaveRequest:request error:&saveError]) {
        NSLog(@"error = %@", saveError);
    }

Apple 支持的回应:

您必须分别保存联系人和群组。然后检查它们是否存在,然后才能将联系人添加到该组。否则,您将收到与输出中相同的错误。

  1. 创建群组
  2. 执行联系人的保存
  3. 检查联系人和组是否都存在
  4. 使用 addMember 将联系人添加到组

这实际上达到了目的,但我不知道为什么我必须实际提出两种类型的请求。

关于ios - 使用联系人框架将联系人添加到群组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46627238/

相关文章:

ios - 联系人图片未获取

android - android中Artoolkitplus获取MarkerId的方法

ios - 使用NSNumberFormatter将9999999格式化为9,999,999(每三位数后的逗号)

android - Xamarin 问题

ios - CNContactViewController 在 contactImage fullscreenView 后隐藏导航栏

ios - 如何仅获取设备上每个联系人的第一个电话号码?

ios - 在 Swift 中声明具有类型约束的属性

ios - 通过 EGO ImageView 延迟加载不适用于自定义单元格

swift - CNContactViewController 上 iOS 13.1 中的键盘覆盖操作表

ios - 将电子邮件添加到 CNMutableContact 错误