本文介绍了CNContactStore通过电子邮件地址检索联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Contacts Framework(IOS 9)来检索与电子邮件地址匹配的联系人.
I would like to use the Contacts Framework (IOS 9) to retrieve contacts that matching an email address.
任何帮助将不胜感激.请使用目标C.谢谢.
Any help will be appreciated.Objective C please.Thanks.
推荐答案
- 添加Contacts.framework
-
#import <Contacts/Contacts.h>
- 编写代码
-(void)loadContactList {
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if( status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted)
{
NSLog(@"access denied");
}
else
{
//Create repository objects contacts
CNContactStore *contactStore = [[CNContactStore alloc] init];
//Select the contact you want to import the key attribute ( https://developer.apple.com/library/watchos/documentation/Contacts/Reference/CNContact_Class/index.html#//apple_ref/doc/constant_group/Metadata_Keys )
NSArray *keys = [[NSArray alloc]initWithObjects:CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, CNContactViewController.descriptorForRequiredKeys, nil];
// Create a request object
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
request.predicate = nil;
[contactStore enumerateContactsWithFetchRequest:request
error:nil
usingBlock:^(CNContact* __nonnull contact, BOOL* __nonnull stop)
{
// Contact one each function block is executed whenever you get
NSString *phoneNumber = @"";
if( contact.phoneNumbers)
phoneNumber = [[[contact.phoneNumbers firstObject] value] stringValue];
NSLog(@"phoneNumber = %@", phoneNumber);
NSLog(@"givenName = %@", contact.givenName);
NSLog(@"familyName = %@", contact.familyName);
NSLog(@"email = %@", contact.emailAddresses);
[contactList addObject:contact];
}];
[contactTableView reloadData];
}
}
这篇关于CNContactStore通过电子邮件地址检索联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!