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

bru*_*h51 7 ios ios9 cncontact cncontactstore contacts-framework

我尝试使用Contacts Framework创建和保存组.首先,用户授权应用程序进行联系人访问.提供了一个viewcontroller,并且+按钮用户显示带有textfield的alertview.

用户键入他想要的组名称,然后单击警报视图的按钮(保存).

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

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>"
)}
Run Code Online (Sandbox Code Playgroud)

接下来奇怪的是:为什么保存请求试图在末尾
用标识符保存这个组:ABGroup

错误包含有关的信息CNInvalidRecords.
我只使用Contacts Framework.
为什么会这样?

有什么解决方案吗?

Pet*_*son 3

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

CNMutableGroup *newGroup = [CNMutableGroup new];
CNSaveRequest *saveRequest = [CNSaveRequest new];
[newGroup setName:self.groupName];
[saveRequest addGroup:newGroup toContainerWithIdentifier:nil];
[contactStore executeSaveRequest:saveRequest error:&error];
Run Code Online (Sandbox Code Playgroud)

并创建了一个新组