4

这是我获取设备联系人并保存到MutableArray.

但我需要recordID单独获取所有联系人并保存到同一个数组中以供进一步使用。(例如,使用 删除联系人recordId)。

请帮助我,我被困了4天。

[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

2 回答 2

4

从您的问题陈述中,我了解到您希望根据该联系人delete联系人从通讯录identifier中找到联系人。当你有identifier那么这就是你需要做的一切:

- (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];
}
于 2016-11-15T14:13:41.487 回答
1

如果您真的需要阅读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
于 2017-07-04T07:04:25.833 回答