ios - 如何从 CNContactStore ios 获取 RecordID

标签 ios objective-c iphone cncontact cncontactstore

这是我获取设备的联系人并保存到 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];
      }]

最佳答案

根据你的问题陈述,我了解到你想根据 identifier 从通讯录中删除一个 contact em>联系。当您拥有 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];
}

关于ios - 如何从 CNContactStore ios 获取 RecordID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585659/

相关文章:

ios - 如果年份小于 1948 年或大于 2086 年,为什么应用程序会崩溃?

ios - CALayer 的实体阴影

ios - 从 View Controller 中的单元格访问 Firebase 数据

ios - 将现有的 iOS 应用程序项目拆分为静态库和应用程序皮肤项目

ios - 如何通过http header 使用基本身份验证从ipad应用程序调用web api服务?

ios - 在表格 View 中添加动态图片

ios - objective-c "callback"

ios - 如何在 < iPhone5 和 swift 中使用长值

iphone - UIScrollView scrollToTop 没有被 iPhone 调用

iphone - 如何在 Objective-C 中使用 OpenCV/或任何移植的库类