CNContactStore retrieve a contact by email address

前端 未结 2 1470
一向
一向 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: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);
       }
    }
    

提交回复
热议问题