2015-09-29 2 views
3

Я хотел бы использовать структуру контактов (IOS 9) для получения контактов, соответствующих адресу электронной почты.CNContactStore получить контакт по электронной почте

Любая помощь будет оценена по достоинству. Цель C пожалуйста. Спасибо.

+0

Я пытаюсь сделать это тоже. Вы выяснили путь? –

+0

Возможно эта страница может вам помочь: https://gist.github.com/willthink/024f1394474e70904728 – Gonzer

ответ

6
  1. Добавить Contacts.framework
  2. #import <Contacts/Contacts.h>
  3. код записи
-(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

Спасибо, что редактировали – minjoongkim

+0

Как найти один контакт под названием «Johnny Appleseed»? – Josh

+0

Как получить все контакты, чье имя «Джон»? – Josh

Смежные вопросы