I am trying to remove URL redundancy in my application. This works well if a single contact has multiple URLs. But if there are connected contacts, as a rule, a failure to work with the message "Operation could not be completed (CNErrorDomain error 2.)"
Any tips on getting around this? Does it work with a single contact? If the removal of the URL should occur separately for individual non-unified elements, is there a way to get them from a single one?
Here is an excerpt from 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) {
NSMutableArray<CNLabeledValue<NSString *> *> *copyOfURLs = [NSMutableArray array];
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);
}
source
share