联系人缺少ios中的一些必需的密钥描述符

时间:2015-10-28 13:09:44

标签: ios ios9 contact

我已使用以下方法

检索所有联系人
- (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);
   }];

}

联系人列在tableview中。现在我试图从``,

中选择特定的联系人
- (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];
}

但它说

  

联系人0x7fc732654530缺少一些必需的密钥描述符

如果有人知道,请帮我解决。感谢。

4 个答案:

答案 0 :(得分:15)

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

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

示例:

{
  "vgearbox_type": "MANUAL",
  "vcondition": "USED",
  "manufacture_year": 1990,
  "page": 1,
  "per_page": 20
}

答案 1 :(得分:3)

改变你的行

 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()])

答案 2 :(得分:1)

您需要将[CNContactViewController descriptorForRequiredKeys]添加为keysToFetch数组,并在CNContactFetchRequest中传递。

示例:

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

答案 3 :(得分:0)

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

如果在没有联系人权限的情况下尝试在从 CNContact 选择的 CNContactViewController 中显示 CNContactPickerViewController,您必须确保 {{1} }} 嵌入在 CNContactViewController 中。

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

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