Inderpal Singh Inderpal Singh - 1 year ago 145
Objective-C Question

How to get RecordID from CNContactStore ios

This is my code to get the device's Contacts and save to


But I need to get the
for all contacts individually and save into the same Array for further use. (For example, to delete Contacts using

Please help me, I'm stuck for 4 days on that.

[contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact* __nonnull contact, BOOL* __nonnull stop){
if( contact.phoneNumbers)
phoneNumber = [[[contact.phoneNumbers firstObject] value]];
if( contact.emailAddresses)
emailAddress = [[contact.emailAddresses firstObject] value];
contactValue=[[NSMutableDictionary alloc] init];
[contactValue setValue:phoneNumber ?:@"" forKey:@"phoneNumber"];
[contactValue setValue:emailAddress ?:@"" forKey:@"emailAddress"];
[contactValue setObject:contact.identifier forKey:@"phoneIdentifier"];
[contactValue setObject:contact.givenName ?:@"" forKey:@"firstName"];
[contactValue setObject:contact.familyName ?:@"" forKey:@"lastName"];

[_totalContact addObject:contactValue];

Answer Source

From your question statement what I understand is that you want to delete a contact from the contact book based on the identifier of that contact. When you have the identifier then this is all you need to do:

- (void)deleteContactWithIdentifier:(NSString *)identifier {

    NSArray *keys = @[CNContactGivenNameKey,
    CNMutableContact *contact = [[store unifiedContactWithIdentifier:identifier keysToFetch:keys error:nil] mutableCopy];
    NSError *error;
    CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
    [saveRequest deleteContact:contact];
    [store executeSaveRequest:saveRequest error:&error];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download