使用联系人框架创建新组,CNErrorDomain Code = 2 [英] create new group with contacts framework, CNErrorDomain Code = 2
问题描述
我尝试使用联系人框架"创建并保存组. 首先,用户授权该应用程序进行联系人访问. 将显示一个视图控制器,并使用 + 按钮向用户显示带有文本字段的警报视图.
i try to create and save a group with the Contacts Framework. First the user authorize the App for contacts access. A viewcontroller is presented and with a + button user shows an alertview with textfield.
用户键入所需的组名,然后单击警报视图的按钮(保存).
The user types the group name he wants and click to button of the alertview (save).
这是保存新组的代码.群组名称可用,但无论如何都无法保存该群组:
This is the code for saving the new group. The group name is available but it is not possible to save this group anyway:
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
?
The next strange thing is: why is the save request trying to save this group
with identifier :ABGroup
at the end?
错误包含有关CNInvalidRecords
的信息.
我仅使用联系人框架.
为什么会这样?
The Error contains a info about CNInvalidRecords
.
I am only using the Contacts Framework.
Why is this happening?
有什么解决办法吗?
推荐答案
使用基本相同的代码对我来说效果很好.
It worked fine for me, with essentially the same code.
CNMutableGroup *newGroup = [CNMutableGroup new];
CNSaveRequest *saveRequest = [CNSaveRequest new];
[newGroup setName:self.groupName];
[saveRequest addGroup:newGroup toContainerWithIdentifier:nil];
[contactStore executeSaveRequest:saveRequest error:&error];
并创建了一个新组
这篇关于使用联系人框架创建新组,CNErrorDomain Code = 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!