4

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

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

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];
      }]
Inder_iOS
  • 1,636
  • 1
  • 12
  • 20

2 Answers2

4

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,
                      CNContactPhoneNumbersKey,
                      CNContactEmailAddressesKey,
                      CNContactIdentifierKey];
    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];
}
Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32
  • Thanks Adeel its work perfect. Can you how to merge duplicate contacts ? – Inder_iOS Nov 15 '16 at 15:15
  • Sure @InderpalSingh. I would suggest that you ask a new question for that as it'll go off topic on this post. You may add the link of the new question here in a comment. – Adeel Miraj Nov 15 '16 at 15:18
  • here is the link @Adeel [link](http://stackoverflow.com/questions/40613589/how-to-merger-the-duplicate-device-contacts-in-ios) – Inder_iOS Nov 15 '16 at 15:25
1

Use this simple extension, if you really need to read recordID (for old API).

recordID always fetched.

This code never submit into App Store. It use a private API!

CNContact+PrivateExtension.h :

NS_ASSUME_NONNULL_BEGIN

@interface CNContact (PrivateExtension)

@property (readonly) NSNumber *privateRecordID;

@end

NS_ASSUME_NONNULL_END

CNContact+PrivateExtension.m :

@implementation CNContact (PrivateExtension)

- (NSNumber *)privateRecordID
{
    return [self valueForKey:@"recordID"];
}

@end
  • Have you used this on iOS 11 beta?, using valueForKey:@"recordID" on the CNContact object on iOS 11 crashes and gives the message "this class is not key value coding-compliant for the key recordID." as if Apple drop the support for ABAddressBook properties – Alfonso Nava Aug 28 '17 at 19:03