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

我尝试使用Contacts Framework创建和保存组。 首先,用户授权应用程序进行联系人访问。 提供了一个视图控制器,并且+按钮用户显示带有文本字段的警报视图。

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

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

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=( //"" [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=( "" )} 

下一个奇怪的事情是:为什么保存请求试图保存该组
带标识符:ABGroup结尾?

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

有什么解决方案吗?

它对我来说很好,基本上是相同的代码。

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

并创建了一个新的团队