5

I am adding contact using Native CNContactViewController and once contact saved it returns contact's identifier with ':ABPerson' suffix and when I cross check in contact list same contact appears with different identifier.

Does anyone know how to get actual contact identifier?

Code to Create:

- (IBAction)didSelectedAddContact:(id)sender {
CNMutableContact *contact =  [CNMutableContact new];

CNContactViewController *contactController = [CNContactViewController viewControllerForNewContact:contact];

NSLog(@"contact id : %@", contact.identifier);

contactController.allowsEditing = true;
contactController.allowsActions = true;

contactController.delegate = self;
[self.navigationController pushViewController:contactController animated:YES];    
}

Delegate Callback:

- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{
_contact = contact;

    [viewController.navigationController popViewControllerAnimated:YES];
}

Below function returns nil:

- (CNContact*) getContactFromStoreForIdentifier:(NSString*) identifier
{
    CNContact *updatedContact = nil;

   id descriptor = [CNContactViewController descriptorForRequiredKeys];

CNContactStore *store = [CNContactStore new];

NSError *error;

updatedContact = [store unifiedContactWithIdentifier:identifier
                                         keysToFetch:@[descriptor]
                                               error:&error];
// Found?
if (updatedContact == nil)
{
    if (error != nil)
    {

    }
}
 return updatedContact; }

@Parameter: Identifier of CNContact object received from didCompleteWithContact: delegate callback.

R.S.
  • 51
  • 4

1 Answers1

0

You have to set a contactStore for the viewController.

CNContactStore *store = [CNContactStore new];
contactController.contactStore = store;

If not this property is not set, than adding the contact to the user's contacts is disabled.

source: https://developer.apple.com/reference/contactsui/cncontactviewcontroller/1616912-contactstore

Jon Rose
  • 8,373
  • 1
  • 30
  • 36
  • Thanks @Jon, But this didn't work as well, finally have to use predicate to get newly added contact from store with permanent contact identifier. – R.S. Mar 17 '17 at 10:03
  • @R.S. I am facing the same issue. how to could you managed here when there were different identifiers? What is meant by permanent contact identifier? – Suresh Durishetti Oct 26 '17 at 08:38