How to get RecordID from CNContactStore ios

This is my code for connecting the device to contacts and saving to MutableArray.

But I need to get recordIDfor all contacts separately and save in the same array for future use. (For example, to remove Contacts using recordID).

Please help me, I am stuck for 4 days.

[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];
      }]
+4
source share
2 answers

From your statement about the question, I understand that you want to deletecontact the contact book based on identifierthis contact. When you have it identifier, then that’s 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];
}
+4

, recordID ( API).

recordID .

App Store. 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
+1

Source: https://habr.com/ru/post/1660669/


All Articles