2

我正在尝试显示仅包含已注册电子邮件的联系人的 ABAddressBook 模式。我如何实现这一目标?

我试过这段代码:

- (IBAction)getContact {
    // creating the picker
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

    for( CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++ ) {
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, emailIndex );
        ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty);
        int emailCount = ABMultiValueGetCount(emailRef);
        if(!emailCount) {
            CFErrorRef error = nil;
            ABAddressBookRemoveRecord(addressBook, person, &error);
            if (error) NSLog(@"Error: %@", error);
        }
    }
    picker.addressBook = addressBook;
    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];
}

该列表显示了我的所有联系人,但“已删除”的联系人显示为“无姓名”,而有姓名的联系人则拥有真实的电子邮件。

4

1 回答 1

2

我设法创建了另一个解决方案......

我在数组中添加了至少有一封电子邮件的联系人......而不是查看当前地址簿并删除其中没有任何电子邮件的解决方案。这是代码:

    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

    for( CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++ ) {
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, emailIndex );
        ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty);
        int emailCount = ABMultiValueGetCount(emailRef);
        if(!emailCount) {
            CFErrorRef error = nil;
            ABAddressBookRemoveRecord(addressBook, person, &error);
            if (error) NSLog(@"Error: %@", error);
        } else {
            ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
            NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);
            NSString *name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));

            if (name) {
                NSMutableDictionary *contactDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                             name, @"name",
                                             email, @"email",
                                             nil];
                [self.contactsArray addObject:contactDict];
            }
        }
    }
于 2012-12-20T20:31:51.080 回答