ios - 使用联系人框架创建新组,CNErrorDomain Code = 2

标签 ios ios9 cncontact cncontactstore contacts-framework

我尝试使用联系人框架创建并保存一个群组。 首先,用户授权应用程序访问联系人。 显示一个 View Controller ,并使用 + 按钮用户显示带有文本字段的警报 View 。

用户键入他想要的组名并单击警报 View 的按钮(保存)。

这是保存新组的代码。组名可用,但无论如何都无法保存该组:

CNContactStore *contactStore = [CNContactStore new];  

[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError *error){
if (granted) {

    CNMutableGroup *newGroup = [CNMutableGroup new];
    CNSaveRequest *saveRequest = [CNSaveRequest new];

    [newGroup setName:groupName];

    //when saving to container with identifier nil, we get this error:
    //Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNInvalidRecords=(
    //"<CNMutableGroup: 0x10a059f20: identifier=2F4981B9-8A47-45A4-8841-1FA5A09584A4:ABGroup, name=gghh>"
    [saveRequest addGroup:newGroup toContainerWithIdentifier:nil];
    [contactStore executeSaveRequest:saveRequest error:&error];

    if (error){
        //error saving group
        //NSLog(@"error message: %@",error);
    } else {
        //if no errors, reload tableview
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }
}
}];



Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNInvalidRecords=(
    "<CNMutableGroup: 0x14fb3e5e0: identifier=8E490585-1223-407E-B353-0D25609B05AB:ABGroup, name=jddjd>"
)}

下一个奇怪的事情是:为什么保存请求试图保存这个组
末尾有标识符 :ABGroup 吗?

错误包含有关 CNInvalidRecords 的信息。
我只使用联系人框架。
为什么会这样?

有什么解决方案吗?

最佳答案

它对我来说效果很好,代码基本相同。

CNMutableGroup *newGroup = [CNMutableGroup new];
CNSaveRequest *saveRequest = [CNSaveRequest new];
[newGroup setName:self.groupName];
[saveRequest addGroup:newGroup toContainerWithIdentifier:nil];
[contactStore executeSaveRequest:saveRequest error:&error];

并创建了一个新组

关于ios - 使用联系人框架创建新组,CNErrorDomain Code = 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33552203/

相关文章:

swift - 使用 Firebase (iOS-Swift) 进行用户读/写数据存储

ios - 在联系人 UI 中显示编辑联系人

ios - 如何在没有过滤器的情况下从设备中检索所有 CNContactStore

ios - 从 xcassets 加载应用程序图标

iOS 7 笔记纸背景

ios - Swift - 火种效果

iOS 创建 Siri 图标和动画

ios - 从已知值创建 CNPostalAddress

swift - UICollectionView 单元格在向上滚动或重新加载功能时随机消失 - iOS 9/Swift

iOS9 - CNContactPickerViewController : pre-select contacts