Ios 使用联系人框架将联系人添加到组
我使用contacts group创建了一个组,然后我想向该组添加一个联系人Ios 使用联系人框架将联系人添加到组,ios,cncontactstore,Ios,Cncontactstore,我使用contacts group创建了一个组,然后我想向该组添加一个联系人 NSPredicate *predicate = [CNGroup predicateForGroupsWithIdentifiers:@[[[NSUserDefaults standardUserDefaults]objectForKey:@"groupIDentifier"]]]; NSArray *groups = [store groupsMatchingPredicate:pr
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);
}
CNGroup *group = [groups firstObject];
[request addMember:contact toGroup:group];
[request addContact:contact toContainerWithIdentifier:nil];
if (![store executeSaveRequest:request error:&saveError]) {
NSLog(@"error = %@", saveError);
}
错误是:
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}
错误=错误域=CNErrorDomain代码=200“更新的记录不存在
Exist“UserInfo={CNInvalidRecordIdentifiers=(
“45FFBB0D-C74B-4A14-8293-9099EA7DEF81:ABGroup”),NSLocalizedDescription=更新的记录不存在,
NSLocalizedFailureReason=保存请求失败,因为它更新了
不存在或已被删除的记录。}
我还尝试使用:
[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}
error=error Domain=CNErrorDomain code=200“更新的记录不存在”UserInfo={CNInvalidRecords=(
“”),NSLocalizedFailureReason=保存请求失败,因为它更新了不存在或已被删除的记录。NSLocalizedDescription=更新的记录不存在}
我发现的一件疯狂的事情是:我需要同时调用addMember
和addContact
,以实际将联系人添加到组中
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);
}
CNGroup *group = [groups firstObject];
[request addMember:contact toGroup:group];
[request addContact:contact toContainerWithIdentifier:nil];
if (![store executeSaveRequest:request error:&saveError]) {
NSLog(@"error = %@", saveError);
}
苹果支持部门的回应:
您必须分别保存联系人和组。然后检查是否存在,然后才能将联系人添加到该组中。否则,您将收到在输出中看到的相同错误
这实际上达到了目的,但我不知道为什么我必须提出这两种类型的请求 我发现一件疯狂的事情是:我需要同时调用
addMember
和addContact
,以实际将联系人添加到组中
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);
}
CNGroup *group = [groups firstObject];
[request addMember:contact toGroup:group];
[request addContact:contact toContainerWithIdentifier:nil];
if (![store executeSaveRequest:request error:&saveError]) {
NSLog(@"error = %@", saveError);
}
苹果支持部门的回应:
您必须分别保存联系人和组。然后检查是否存在,然后才能将联系人添加到该组中。否则,您将收到在输出中看到的相同错误
这实际上达到了目的,但我不知道为什么我必须提出这两种类型的请求 他们有没有告诉你如果3次失败该怎么办?你是否继续尝试?他们有没有告诉你如果3次失败该怎么办?你是否继续尝试?