美文网首页
iOS-使用CNContactViewController的问题

iOS-使用CNContactViewController的问题

作者: 邢罗康 | 来源:发表于2023-03-15 14:03 被阅读0次



在tableView列表中显示自定义UI的联系人,当选中联系跳转显示时崩溃并出现错误:

*** Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'Contact 0x101331a70 is missing some of the required key descriptors: (
    "<CNAggregateKeyDescriptor: 0x28276fa80: kind=+[CNContactContentViewController descriptorForRequiredKeys]>"
)'
terminating with uncaught exception of type NSException

联系人数组是:

@property(nonatomic,strong)NSMutableArray<CNContact *> * contactArray;

当选中其中一行索引时,我试图通过这样做来显示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CNContact * contact = [self.dataArray objectAtIndex:indexPath.row];

    CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
    vc.delegate = self;
    vc.displayedPropertyKeys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    [vc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:vc animated:YES];
}

崩溃信息显示:缺少descriptorForRequiredKeys 传递给数组

解决方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CNContact * contact = [self.dataArray objectAtIndex:indexPath.row];
    
    CNContactStore *store = [[CNContactStore alloc] init];
    NSError *error = nil;
    contact = [store unifiedContactWithIdentifier:contact.identifier
                            keysToFetch:@[[CNContactViewController descriptorForRequiredKeys]]
                                  error:&error];

    CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
    vc.delegate = self;
    vc.displayedPropertyKeys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    [vc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:vc animated:YES];
}





问题2: [CNUI ERROR] Contact view delayed appearance timed out

弹出联系人列表,选中行索引显示联系人详情时,无反应。

[CNUI ERROR] Contact view delayed appearance timed out
[CNUI ERROR] Contact view delayed appearance timed out
[CNUI ERROR] Contact view delayed appearance timed out
......

代码:

// [选择联系人]
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
    CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
    vc.delegate = self;
    vc.displayedPropertyKeys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    [self presentViewController: vc animated:YES completion:nil];
}

解决方法:

// [选择联系人]
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
    CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
    vc.delegate = self;
    vc.displayedPropertyKeys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    
    //将vc包装进UINavigationController
    UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:vc];
    
    __weak SecondMethodViewController * weakSelf = self;
    
    //出现延迟:[CNUI ERROR] Contact view delayed appearance timed out
    //通知主线程刷新UI
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf presentViewController:navi animated:YES completion:nil];
    });
}

相关文章

网友评论

      本文标题:iOS-使用CNContactViewController的问题

      本文链接:https://www.haomeiwen.com/subject/daulrdtx.html