联系人在ios中缺少一些必需的键描述符 [英] Contact is missing some of the required key descriptors in ios

查看:103
本文介绍了联系人在ios中缺少一些必需的键描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have retrieves all contact by using following method

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

}

表视图。现在我正在尝试从``,

And contacts are listed in tableview. Now I am trying to select particular contact from ``,

- (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缺少一些必需的键描述符

Contact 0x7fc732654530 is missing some of the required key descriptors

如果有,请帮我解决任何人都知道。谢谢。

Please help me to solve it, if anyone know. Thanks.

推荐答案

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

Add "CNContactViewController.descriptorForRequiredKeys" at the end of your keysToFetch array.

这样做后为我工作。

示例:

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

这篇关于联系人在ios中缺少一些必需的键描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆