CNContactStore retrieve a contact by email address

前端 未结 2 1469
一向
一向 2021-01-13 12:18

I would like to use the Contacts Framework (IOS 9) to retrieve contacts that matching an email address.

Any help will be appreciated. Objective C please. Thanks.

相关标签:
2条回答
  • 2021-01-13 12:32
    1. Add Contacts.framework
    2. #import <Contacts/Contacts.h>
    3. write code
    -(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];
        }
    
    }
    
    0 讨论(0)
  • 2021-01-13 12:56

    Old question, but I stumbled on it looking to do this too. Sharing how to do this on iOS11 and above.

    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 讨论(0)
提交回复
热议问题