ios - ABAddressBookCopyArrayOfAllPeople 不返回任何人

标签 ios xcode abaddressbook

我开始使用 ABAddress Book 并使用一个非常简单的起点...我想获取我的地址簿中的所有条目并将其放入一个数组中。它一直显示 0 个元素。

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

ABAddressBookCopyArrayOfAllPeople 实际上不是从您的联系人列表中获取所有人,还是我误解了什么。

管理员注意事项 我的问题与 Objective C 相关,与 SWIFT 无关,因此此答案不适用。在推荐的答案中,它解释了为什么 OP 从 Objective C 到 Swift 的代码迁移存在缺陷并且不起作用。我的问题不是。我试图理解为什么当我在我的 iPhone 上安装它时,它有数百个联系人 allContacts 没有任何项目。我试图了解将所有联系人放入数组的正确方法。主要是因为业务需要,我正在做自己的联系人选择器,因为我不想使用 Apple 的内置联系人选择器。

最佳答案

首先,您正在尝试使用已弃用的 api。这可能就是为什么 ABAddressBookCreateWithOptions不起作用。

但是,根据您的编辑(您说要构建自己的 UI),我的建议是使用 Contacts framework .

参见 this github gist :

#import <Contacts/Contacts.h>

@implementation ContactsScan

- (void) contactScan
{
    if ([CNContactStore class]) {
        //ios9 or later
        CNEntityType entityType = CNEntityTypeContacts;
        if( [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusNotDetermined)
         {
             CNContactStore * contactStore = [[CNContactStore alloc] init];
             [contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
                 if(granted){
                     [self getAllContact];
                 }
             }];
         }
        else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized)
        {
            [self getAllContact];
        }
    }
}

-(void)getAllContact
{
    if([CNContactStore class])
    {
        //iOS 9 or later
        NSError* contactError;
        CNContactStore* addressBook = [[CNContactStore alloc]init];
        [addressBook containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[addressBook.defaultContainerIdentifier]] error:&contactError];
        NSArray * keysToFetch =@[CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey];
        CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
        BOOL success = [addressBook enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){
            [self parseContactWithContact:contact];
        }];
    }
}

- (void)parseContactWithContact :(CNContact* )contact
{
    NSString * firstName =  contact.givenName;
    NSString * lastName =  contact.familyName;
    NSString * phone = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"];
    NSString * email = [contact.emailAddresses valueForKey:@"value"];
    NSArray * addrArr = [self parseAddressWithContac:contact];
}

- (NSMutableArray *)parseAddressWithContac: (CNContact *)contact
{
    NSMutableArray * addrArr = [[NSMutableArray alloc]init];
    CNPostalAddressFormatter * formatter = [[CNPostalAddressFormatter alloc]init];
    NSArray * addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"];
    if (addresses.count > 0) {
        for (CNPostalAddress* address in addresses) {
            [addrArr addObject:[formatter stringFromPostalAddress:address]];
        }
    }
    return addrArr;
}

@end

注意:这段代码不是我写的。来源可以在 github 上找到.

关于ios - ABAddressBookCopyArrayOfAllPeople 不返回任何人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314534/

相关文章:

ios - 按关系属性对 NSFetchedResultsController 排序? NSFetchRequest NSSortDescriptor

ios - 在 iOS XCode 项目中管理大量目标预处理器宏

ios - 以编程方式将 iPhone 联系人导出到 .vcf 文件

ios - 访问用户地址簿的权限警报不会停止执行

ios - 使用 ABAddressBookRegisterExternalChangeCallback 注册的地址簿更改回调永远不会被调用 (iOS 8)

ios - 如何偏移空 TableView 中显示的消息

ios - 线程 1 在放置按钮或标签 iOS 时发出 SIGABRT 错误信号

iphone - 在 iOS 6 中使用格式输入 UITextView

ios - Xcode 无法获得相同的颜色

ios - swift 4 : Mask not working for UIImage?