我想获取所有没有过滤器的联系人及其 key 的所有信息。
func unifiedContactsMatchingPredicate(_ predicate: NSPredicate,
keysToFetch keys: [CNKeyDescriptor]) throws -> [CNContact]
我正在使用上述方法来获取联系人。
我必须指定所有键吗? 他们是否有任何快捷方式可以在不指定所有 key 的情况下获取所有 key 信息?
最佳答案
没有。您必须自己指定所有必需的 key 。请查看苹果文档here **编辑:**请尝试此方法:
-(void)getAllContacts
{
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
NSString *containerId = store.defaultContainerIdentifier;
//to fetch all contacts
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
//to fetch contacts with matching name
// NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"vishal"];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
for (CNContact *contact in cnContacts) {
// copy data to my custom Contacts class.
Contact *newContact = [[Contact alloc] init];
newContact.firstName = contact.givenName;
newContact.lastName = contact.familyName;
UIImage *image = [UIImage imageWithData:contact.imageData];
newContact.image = image;
for (CNLabeledValue *label in contact.phoneNumbers) {
NSString *phone = [label.value stringValue];
if ([phone length] > 0) {
[newContact.phones addObject:phone];
}
}
[contacts addObject:newContact];
}
}
}
关于ios - 使用新的联系人框架获取所有联系人和所有 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450643/