CNContactStore executeSaveRequest failing with (CNErrorDomain error 2.)

I'm trying to remove redundancy of URLs to my application. This works well, if a single contact has several URLs. But if there are linked contacts, typically save operation fails with the message

"The operation couldn’t be completed. (CNErrorDomain error 2.)"


Any advice, how to work this around? Doesn't it work with unified contact? If remove of an URL should happen separately on individual non-unified items, is there a way to get those from unified one?


Here is excerpt of the code:

CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];

NSArray* keys = @[CNContactUrlAddressesKey];
NSPredicate *predicate = [CNContact predicateForContactsWithIdentifiers:identifiers];
NSError *error;
NSArray <CNContact *> *contacts = [_contactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

for (CNContact *contact in contacts) {
 // here we'll collect non-ambigous URLs
  NSMutableArray<CNLabeledValue<NSString *> *> *copyOfURLs = [NSMutableArray array];
  // just a marker for the moment if a URL with specific prefix was already found

  NSString *baseURL = nil;
  for (CNLabeledValue<NSString *> *labeledValue in contact.urlAddresses) {
    NSString *url = labeledValue.value;
    if ([url hasPrefix:APP_IDENTITY_URL_SCHEME]) {
       if (baseURL == nil)
         baseURL = url;
       else
         continue;
     }
     [copyOfURLs addObject:labeledValue];
  }
  CNMutableContact *updatedContact = [contact mutableCopy];
  updatedContact.urlAddresses = copyOfURLs;
  [saveRequest updateContact:updatedContact];
}
NSError *saveError;
if (![_contactStore executeSaveRequest:saveRequest error:&saveError]) {
  NSLog(@"Saving error: %@", saveError.localizedDescription);
}


Thanks in advance!

Replies

Did you ever figure this out? I'm getting the same error.