首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CNContactStore按电子邮件地址检索联系人

CNContactStore按电子邮件地址检索联系人
EN

Stack Overflow用户
提问于 2015-09-29 12:46:18
回答 2查看 6.2K关注 0票数 4

我想使用联系人框架(IOS 9)来检索与电子邮件地址匹配的联系人。

任何帮助都将不胜感激。目标C。谢谢。

EN

回答 2

Stack Overflow用户

发布于 2016-01-07 17:29:53

  1. Add Contacts.framework
  2. #import <Contacts/Contacts.h>
  3. write

(为添加代码)

代码语言:javascript
复制
-(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];
    }

}
票数 8
EN

Stack Overflow用户

发布于 2019-11-15 22:09:11

这是个老问题,但我也是偶然发现的。在iOS11和更高版本上分享如何做到这一点。

代码语言:javascript
复制
NSString* emailAddress = @"John-Appleseed@mac.com";
//Create repository objects contacts
CNContactStore *store = [[CNContactStore alloc] init];

NSPredicate* predicate = [CNContact predicateForContactsMatchingEmailAddress:emailAddress];
NSArray* keysToFetch = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
NSError *error;
NSArray *contacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keysToFetch error:&error];
if (error) {
       NSLog(@"error fetching contacts %@", error);
} else {
   for (CNContact *contact in contacts) {
       NSLog(@"%@", contact.givenName);
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32835853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档