ios 中的联系人缺少一些必需的关键描述符

Contact is missing some of the required key descriptors in ios

我已经使用以下方法检索了所有联系人

- (void)getAllContacts:(void(^)(NSArray *array))handler
{
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

    if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusDenied)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can use contacts" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:TRUE completion:nil];
        return;
    }

    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

        // make sure the user granted us access
        if (!granted)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                // user didn't grant access;
                // so, again, tell user here why app needs permissions in order  to do it's job;
                // this is dispatched to the main queue because this request could be running on background thread
            });
            return;
        }

        // build array of contacts
        NSMutableArray *contacts = [NSMutableArray array];

        NSError *fetchError;
        CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]]];

        BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {

            [contacts addObject:contact];
        }];

        if (!success)
        {
            NSLog(@"error = %@", fetchError);
            return;
        }
        handler((NSArray *)contacts);
   }];

}

联系人列在表格视图中。现在我正在尝试 select 来自 ``,

的特定联系人
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CNContact *contact = [arrContacts objectAtIndex:indexPath.row];
    NSArray *keys = @[CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]];
    CNContactViewController *contactController = [CNContactViewController viewControllerForContact:contact];
    contactController.delegate = self;
    contactController.allowsEditing = YES;
    contactController.allowsActions = YES;
    contactController.displayedPropertyKeys = keys;
    [self.navigationController pushViewController:contactController animated:TRUE];
}

但是它说

Contact 0x7fc732654530 is missing some of the required key descriptors

有知道的请帮我解决一下。谢谢

您需要添加 [CNContactViewController descriptorForRequiredKeys] 作为 keysToFetch 的数组,您必须传入 CNContactFetchRequest

示例:

CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName], [CNContactViewController descriptorForRequiredKeys]]];

在 keysToFetch 数组的末尾添加 "CNContactViewController.descriptorForRequiredKeys"。

在我这样做之后为我工作。

示例:

NSArray *keysToFetch = @[CNContactIdentifierKey, CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey, CNContactImageDataKey, CNContactViewController.descriptorForRequiredKeys];

换你的线

 CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]]];

 CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName],[CNContactViewController descriptorForRequiredKeys]]];

有些人在 swift 中使用它然后添加

let request:CNContactFetchRequest
request = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactEmailAddressesKey,CNContactPhoneNumbersKey,CNContactViewController.descriptorForRequiredKeys()])

我不确定这是 Apple 错误处理的错误还是我缺乏理解,但我的解决方案与密钥描述符完全无关。

如果要在 CNContactViewController 中显示 CNContactCNContactPickerViewController 中选择的 CNContactViewController 没有 联系权限,您必须确保 CNContactViewController 嵌入在 UINavigationController.

在我的例子中,我还必须向控制器添加左和右 UIBarButtonItem 来处理解雇。请注意,如果您将 CNContactViewController 推到 UINavigationController 上,这可能不是必需的,但是因为我使用的是 present 方法,所以这是必要的。

希望这能帮助其他人解决这个神秘的错误!