Ios 列出有电话号码的联系人

Ios 列出有电话号码的联系人,ios,objective-c,contacts,Ios,Objective C,Contacts,我只想获取(在iOS中使用Objective-C)有电话号码的联系人,但我怎么做?我试图像下面的代码那样构造谓词,但显然不起作用 contacts = [contactStore unifiedContactsMatchingPredicate:[NSPredicate predicateWithFormat:@"phoneNumbers <> nil"] keysToFetch:KEYS error:nil]; contacts=[contactStore unifiedCont

我只想获取(在iOS中使用Objective-C)有电话号码的联系人,但我怎么做?我试图像下面的代码那样构造谓词,但显然不起作用

contacts = [contactStore unifiedContactsMatchingPredicate:[NSPredicate predicateWithFormat:@"phoneNumbers <> nil"] keysToFetch:KEYS error:nil];
contacts=[contactStore unifiedContactsMatchingPredicate:[NSPredicate predictewithformat:@“phoneNumbers nil”]keystefeatch:KEYS错误:nil];

那么,正确的做法是什么呢?谢谢你的帮助

使用以下方法导入

#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h> 


-(void)contactsDetailsFromPhoneContactBook{
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            //keys with fetching properties
            NSArray *keys = @[CNContactFamilyNameKey,CNContactGivenNameKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
            if (error) {
                NSLog(@"error fetching contacts %@", error);
            } else {
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;
                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }
                    [self.contactsArray addObject:fullName];

                    NSLog(@"working or not %@",self.contactsArray);
                }

            }
        }
    }];
}
#导入
#进口
-(无效)通讯录中的通讯录详细信息{
CNContactStore*store=[[CNContactStore alloc]init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL已授予,n错误*\u可为空错误){
如果(已授予==是){
//具有抓取属性的键
NSArray*密钥=@[CNContactFamilyNameKey,CNContactGivenNameKey];
NSString*containerId=store.defaultContainerIdentifier;
NSPredicate*谓词=[CNContact谓词ForContactsInContainerWithiIdentifier:containerId];
n错误*错误;
NSArray*cnContacts=[store unifiedContactsMatchingPredicate:predicate keystefetch:keys error:&error];
如果(错误){
NSLog(@“获取联系人时出错%@”,错误);
}否则{
NSString*全名;
NSString*名字;
NSString*lastName;
用于(CNContact*cnContacts中的联系人){
//将数据复制到我的自定义联系人类。
firstName=contact.givenName;
lastName=contact.familyName;
如果(lastName==nil){
fullName=[NSString stringWithFormat:@“%@”,firstName];
}else if(firstName==nil){
全名=[NSString stringWithFormat:@“%@”,lastName];
}
否则{
fullName=[NSString stringWithFormat:@“%@%@”,firstName,lastName];
}
[self.contactsArray addObject:fullName];
NSLog(@“工作或不工作%@”,自我联系协议);
}
}
}
}];
}

使用以下方法导入

#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h> 


-(void)contactsDetailsFromPhoneContactBook{
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            //keys with fetching properties
            NSArray *keys = @[CNContactFamilyNameKey,CNContactGivenNameKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
            if (error) {
                NSLog(@"error fetching contacts %@", error);
            } else {
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;
                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }
                    [self.contactsArray addObject:fullName];

                    NSLog(@"working or not %@",self.contactsArray);
                }

            }
        }
    }];
}
#导入
#进口
-(无效)通讯录中的通讯录详细信息{
CNContactStore*store=[[CNContactStore alloc]init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL已授予,n错误*\u可为空错误){
如果(已授予==是){
//具有抓取属性的键
NSArray*密钥=@[CNContactFamilyNameKey,CNContactGivenNameKey];
NSString*containerId=store.defaultContainerIdentifier;
NSPredicate*谓词=[CNContact谓词ForContactsInContainerWithiIdentifier:containerId];
n错误*错误;
NSArray*cnContacts=[store unifiedContactsMatchingPredicate:predicate keystefetch:keys error:&error];
如果(错误){
NSLog(@“获取联系人时出错%@”,错误);
}否则{
NSString*全名;
NSString*名字;
NSString*lastName;
用于(CNContact*cnContacts中的联系人){
//将数据复制到我的自定义联系人类。
firstName=contact.givenName;
lastName=contact.familyName;
如果(lastName==nil){
fullName=[NSString stringWithFormat:@“%@”,firstName];
}else if(firstName==nil){
全名=[NSString stringWithFormat:@“%@”,lastName];
}
否则{
fullName=[NSString stringWithFormat:@“%@%@”,firstName,lastName];
}
[self.contactsArray addObject:fullName];
NSLog(@“工作或不工作%@”,自我联系协议);
}
}
}
}];
}
#导入
CNContactStore*store=[[CNContactStore alloc]init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL已授予,n错误*\u可为空错误){
如果(已授予==是){
//具有抓取属性的键
NSArray*keys=@[CNContactFamilyNameKey、CNContactGivenNameKey、CNContactPhoneNumbersKey、CNContactImageDataKey、CNContactEmailAddressesKey];
CNContactFetchRequest*请求=[[CNContactFetchRequest alloc]initWithKeyFetch:keys];
request.sortOrder=CNContactSortOrderGivenName;
request.unifyResults=是;
n错误*错误;
__阻止NSString*电子邮件;
BOOL success=[store enumerateContactsWithFetchRequest:请求错误:&错误使用block:^(CNContact*\uuuuu非空联系人,BOOL*\uuu非空停止)
{
如果(错误){
NSLog(@“获取联系人时出错%@”,错误);
}否则{
NSString*全名;
NSString*电话;
//用于(CNContact*cnContacts中的联系人){
DeviceContact*aContact=[DeviceContact new];
//将数据复制到我的自定义联系人类。
NSString*firstName=contact.givenName;
NSString*lastName=contact.familyName;
如果(lastName==nil){
fullName=[NSString stringWithFormat:@“%@”,firstName];
}else if(firstName==nil){
全名=[NSString stringWithFormat:@“%@”,lastName];
}