sincronizar recuperar por pasar cómo contactos ios ios9 nspredicate contacts

ios - por - recuperar contactos iphone 6



Cómo recuperar todos los registros de contactos en iOS 9 usando el Marco de contactos (19)

¡La respuesta de @rocolitis es rápida! Su respuesta es la forma más correcta de hacerlo de acuerdo con la documentación de Apple.

let contactStore = CNContactStore() let keys = [CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactNicknameKey] as [CNKeyDescriptor] let request = CNContactFetchRequest(keysToFetch: keys) try? contactStore.enumerateContacts(with: request) { (contact, error) in // Do something with contact }

¡Probablemente deberías verificar tu acceso a tus contactos primero!

let authorization = CNContactStore.authorizationStatus(for: CNEntityType.contacts) switch authorization { case .authorized: break case .denied: break case .restricted: break case .notDetermined: break }

La mayor parte del marco de AddressBook está en desuso en iOS 9. En la nueva documentation Marco de contactos solo se muestra cómo recuperar registros que coinciden con NSPredicate , pero ¿qué NSPredicate si quiero todo el registro?


Apple realmente recomienda enumerateContactsWithFetchRequest de CNContactStore para obtener todos los contactos y NO unificarContactsMatchingPredicate.

A continuación se muestra el código de trabajo para Obj-C.

CNContactStore *store = [[CNContactStore alloc] init]; //keys with fetching properties NSArray *keys = @[CNContactGivenNameKey, CNContactPhoneNumbersKey]; CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys]; NSError *error; [store enumerateContactsWithFetchRequest:request error:&error usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop) { // access it this way -> contact.givenName; etc }];

Aquí está el enlace donde Apple recomienda enumerar la función: https://developer.apple.com/reference/contacts/cncontactstore/1403266-unifiedcontactsmatchingpredicate?language=objc#discussion

Si el enlace expiró, esto es lo que Apple escribió:

Si no se encuentran coincidencias, este método devuelve una matriz vacía (o nula en caso de error). Utilice solo los predicados de los predicados de la clase CNContact. Los predicados compuestos no son compatibles con este método. Debido a la unificación, los contactos devueltos pueden tener identificadores diferentes de los que especifique. Para buscar todos los contactos , use enumerateContactsWithFetchRequest:error:usingBlock:


Aquí está la versión swift 3.0 de la respuesta de flohei

lazy var contacts: [CNContact] = { let contactStore = CNContactStore() let keysToFetch = [ CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPostalAddressesKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey, CNContactThumbnailImageDataKey] as [Any] // Get all the containers var allContainers: [CNContainer] = [] do { allContainers = try contactStore.containers(matching: nil) } catch { print("Error fetching containers") } var results: [CNContact] = [] // Iterate all containers and append their contacts to our results array for container in allContainers { let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier) do { let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch as! [CNKeyDescriptor]) results.append(contentsOf: containerResults) } catch { print("Error fetching results for container") } } return results }()

¡Espero que esto ayude!


C objetivo:

//ios 9+ CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { //keys with fetching properties NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; NSString *containerId = store.defaultContainerIdentifier; NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; NSError *error; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; if (error) { NSLog(@"error fetching contacts %@", error); } else { for (CNContact *contact in cnContacts) { // copy data to my custom Contacts class. Contact *newContact = [[Contact alloc] init]; newContact.firstName = contact.givenName; newContact.lastName = contact.familyName; UIImage *image = [UIImage imageWithData:contact.imageData]; newContact.image = image; for (CNLabeledValue *label in contact.phoneNumbers) { NSString *phone = [label.value stringValue]; if ([phone length] > 0) { [contact.phones addObject:phone]; } } } } } }];

También para obtener todos los contactos, puede usar el método enumerateContactsWithFetchRequest :

CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { //keys with fetching properties NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys]; NSError *error; BOOL success = [store enumerateContactsWithFetchRequest:request error:&error usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop) { if (error) { NSLog(@"error fetching contacts %@", error); } else { // copy data to my custom Contact class. Contact *newContact = [[Contact alloc] init]; newContact.firstName = contact.givenName; newContact.lastName = contact.familyName; // etc. } }]; } }];

Si desea filtrar los contactos por nombre , puede usar esto:

Obj-C:

// keys from example above NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:[CNContact predicateForContactsMatchingName:@"John Appleseed"] keysToFetch:keys error:&error];

Swift 3:

let store = CNContactStore() let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("Appleseed"), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey])

La documentación oficial está aquí: https://developer.apple.com/reference/contacts


CNContact en iOS 9

C objetivo

#import "ViewController.h" #import <Contacts/Contacts.h> @interface ViewController () { NSMutableArray *arrayTableData; } @end @implementation ViewController -(void)viewDidLoad { [self fetchContactsandAuthorization]; } //This method is for fetching contacts from iPhone.Also It asks authorization permission. -(void)fetchContactsandAuthorization { // Request authorization to Contacts CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { //keys with fetching properties NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; NSString *containerId = store.defaultContainerIdentifier; NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; NSError *error; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; if (error) { NSLog(@"error fetching contacts %@", error); } else { NSString *phone; NSString *fullName; NSString *firstName; NSString *lastName; UIImage *profileImage; NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init]; for (CNContact *contact in cnContacts) { // copy data to my custom Contacts class. firstName = contact.givenName; lastName = contact.familyName; if (lastName == nil) { fullName=[NSString stringWithFormat:@"%@",firstName]; }else if (firstName == nil){ fullName=[NSString stringWithFormat:@"%@",lastName]; } else{ fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName]; } UIImage *image = [UIImage imageWithData:contact.imageData]; if (image != nil) { profileImage = image; }else{ profileImage = [UIImage imageNamed:@"person-icon.png"]; } for (CNLabeledValue *label in contact.phoneNumbers) { phone = [label.value stringValue]; if ([phone length] > 0) { [contactNumbersArray addObject:phone]; } } NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil]; [arrayTableData addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]]; NSLog(@"The contactsArray are - %@",arrayTableData); } dispatch_async(dispatch_get_main_queue(), ^{ [tableViewContactData reloadData]; }); } } }]; } @end

La salida es

The contactsArray are - ( "John Appleseed", "Kate Bell", "Anna Haro", "Daniel Higgins", "David Taylor", "Hank Zakroff" }


En swift 3 and Xcode 8 puede obtener la lista de todos los contactos

let keys = [CNContactGivenNameKey ,CNContactImageDataKey,CNContactPhoneNumbersKey] var message: String! //let request=CNContactFetchRequest(keysToFetch: keys) let contactsStore = AppDelegate.AppDel.contactStore // Get all the containers var allContainers: [CNContainer] = [] do { allContainers = try contactsStore.containers(matching: nil) } catch { print("Error fetching containers") } // Iterate all containers and append their contacts to our results array for container in allContainers { let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier) do { let containerResults = try contactsStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keys as [CNKeyDescriptor]) self.results.append(contentsOf: containerResults) self.tableView.reloadData() message="/(self.results.count)" } catch { print("Error fetching results for container") } }


En este momento en iOS9 ABAddressBookRef está en desuso, por lo que para obtener todos los contactos del teléfono, use este marco y agregue esta función, obtendrá una matriz de contactos.

Importar marco de contacto en clase .h como esta

#import <Contacts/Contacts.h>

luego agregue este método en el archivo .m

-(void)contactsFromAddressBook{ //ios 9+ CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { //keys with fetching properties NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; NSString *containerId = store.defaultContainerIdentifier; NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; NSError *error; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; if (error) { NSLog(@"error fetching contacts %@", error); } else { NSString *phone; NSString *fullName; NSString *firstName; NSString *lastName; UIImage *profileImage; NSMutableArray *contactNumbersArray; for (CNContact *contact in cnContacts) { // copy data to my custom Contacts class. firstName = contact.givenName; lastName = contact.familyName; if (lastName == nil) { fullName=[NSString stringWithFormat:@"%@",firstName]; }else if (firstName == nil){ fullName=[NSString stringWithFormat:@"%@",lastName]; } else{ fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName]; } UIImage *image = [UIImage imageWithData:contact.imageData]; if (image != nil) { profileImage = image; }else{ profileImage = [UIImage imageNamed:@"person-icon.png"]; } for (CNLabeledValue *label in contact.phoneNumbers) { phone = [label.value stringValue]; if ([phone length] > 0) { [contactNumbersArray addObject:phone]; } } NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil]; [MutableArray__Contact addObject:personDict]; } dispatch_async(dispatch_get_main_queue(), ^ { NSLog(@"%@",ar_Contact); //[self.tableViewRef reloadData]; }); } } }]; }

para usar este método llame a la función contactosFromAddressBook

[self contactsFromAddressBook];


Estoy intentando este código, funciona bien. Puedo obtener todos los detalles de contactos usando este código en el último marco de swift3 usando contactos:

let requestForContacts = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey as CNKeyDescriptor, CNContactFormatter.descriptorForRequiredKeys(for: CNContactFormatterStyle.fullName), CNContactPhoneNumbersKey as CNKeyDescriptor ,CNContactImageDataKey as CNKeyDescriptor,CNContactEmailAddressesKey as CNKeyDescriptor,CNContactBirthdayKey as CNKeyDescriptor]) do { try self.store.enumerateContacts(with: requestForContacts) { contact, stop in print("contact:/(contact)") self.contacts.append(contact) } } catch { print(error) } for contact in self.contacts { print(contact) let firstName = contact.givenName nameArray.append(firstName) print("first:/(firstName)") let phoneNumber = (contact.phoneNumbers[0].value).value(forKey: "digits") phoneNumberArray.append(phoneNumber as! String) let emailAddress = contact.emailAddresses[0].value(forKey: "value") emailAddressArray.append(emailAddress as! String) }


La respuesta de Cody en Swift 3:

import Contacts

Luego, dentro de cualquier función que esté utilizando:

let store = CNContactStore() store.requestAccess(for: .contacts, completionHandler: { granted, error in guard granted else { let alert = UIAlertController(title: "Can''t access contact", message: "Please go to Settings -> MyApp to enable contact permission", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) self.present(alert, animated: true, completion: nil) return } let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey] as [Any] let request = CNContactFetchRequest(keysToFetch: keysToFetch as! [CNKeyDescriptor]) var cnContacts = [CNContact]() do { try store.enumerateContacts(with: request){ (contact, cursor) -> Void in cnContacts.append(contact) } } catch let error { NSLog("Fetch contact error: /(error)") } print(">>>> Contact list:") for contact in cnContacts { let fullName = CNContactFormatter.string(from: contact, style: .fullName) ?? "No Name" print("/(fullName): /(contact.phoneNumbers.description)") } })


Las otras dos respuestas solo cargan contactos desde el contenedor con defaultContainerIdentifier . En un escenario, donde el usuario tiene más de un contenedor (es decir, una cuenta de Exchange y una cuenta de iCloud que se utilizan para almacenar contactos), esto solo cargaría los contactos de la cuenta que está configurada como predeterminada. Por lo tanto, no cargaría todos los contactos según lo solicitado por el autor de la pregunta.

Lo que probablemente querrá hacer es obtener todos los contenedores e iterar sobre ellos para extraer todos los contactos de cada uno de ellos. El siguiente fragmento de código es un ejemplo de cómo lo hacemos en una de nuestras aplicaciones (en Swift):

lazy var contacts: [CNContact] = { let contactStore = CNContactStore() let keysToFetch = [ CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey, CNContactThumbnailImageDataKey] // Get all the containers var allContainers: [CNContainer] = [] do { allContainers = try contactStore.containersMatchingPredicate(nil) } catch { print("Error fetching containers") } var results: [CNContact] = [] // Iterate all containers and append their contacts to our results array for container in allContainers { let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier) do { let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch) results.appendContentsOf(containerResults) } catch { print("Error fetching results for container") } } return results }()


Solo quería compartir estas versiones de Swift 4

info.plist:

<key>NSContactsUsageDescription</key> <string>$(PRODUCT_NAME) requires to access your contacts ...</string>

módulo:

import Contacts

código:

func fetchContacts(completion: @escaping (_ result: [CNContact]) -> Void){ DispatchQueue.main.async { var results = [CNContact]() let keys = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactMiddleNameKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey] as [CNKeyDescriptor] let fetchRequest = CNContactFetchRequest(keysToFetch: keys) fetchRequest.sortOrder = .userDefault let store = CNContactStore() store.requestAccess(for: .contacts, completionHandler: {(grant,error) in if grant{ do { try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in results.append(contact) }) } catch let error { print(error.localizedDescription) } completion(results) }else{ print("Error /(error?.localizedDescription ?? "")") } }) } }

Uso:

fetchContacts(completion: {contacts in contacts.forEach({print("Name: /($0.givenName), number: /($0.phoneNumbers.first?.value.stringValue ?? "nil")")})

Primero debe describir la información de uso en info.plist. He agregado una verificación para determinar que el usuario ha otorgado el acceso a los contactos y luego definí las claves (los valores necesarios para ser recuperados). Como se dijo en una de las respuestas anteriores, es un proceso lento, por lo que agregué DispatchQueue para el procesamiento en segundo plano y el controlador de finalización para devolver la matriz de contactos a la persona que llama.


Swift 4.2. Obtener números de contacto con imagen

info.plist file data <key>NSContactsUsageDescription</key> <string>$(PRODUCT_NAME) requires to access your contacts ...</string> //MARK:- Fetch All Contacts of Phone func fetchContacts(completion: @escaping (_ result: [CNContact]) -> Void){ DispatchQueue.main.async { var results = [CNContact]() let keys = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactMiddleNameKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey,CNContactThumbnailImageDataKey] as [CNKeyDescriptor] let fetchRequest = CNContactFetchRequest(keysToFetch: keys) fetchRequest.sortOrder = .userDefault let store = CNContactStore() store.requestAccess(for: .contacts, completionHandler: {(grant,error) in if grant{ do { try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in results.append(contact) }) } catch let error { print(error.localizedDescription) } completion(results) }else{ print("Error /(error?.localizedDescription ?? "")") } }) } }

}

Llamada de función en método de carga

var arrpic = NSMutableArray() override func viewDidLoad() { super.viewDidLoad() fetchContacts(completion: {contacts in contacts.forEach({print("Name: /($0.givenName), number: /($0.phoneNumbers.first?.value.stringValue ?? "nil")") self.arrfname.append("/($0.givenName)") self.arrlname.append("/($0.familyName)") self.arrnumber.append("/($0.phoneNumbers.first?.value.stringValue ?? "nil")") var img = UIImage() if $0.thumbnailImageData != nil { img = UIImage.init(data: $0.thumbnailImageData!)! self.arrpic.add(img) } else { self.arrpic.add("") } }) if contacts.count > 0 { self.tablev.reloadData() } }) }


Uso del marco Swift y Contactos para buscar todos los contactos, incluidos el nombre y los números de teléfono

import Contacts let store = CNContactStore() store.requestAccessForEntityType(.Contacts, completionHandler: { granted, error in guard granted else { let alert = UIAlertController(title: "Can''t access contact", message: "Please go to Settings -> MyApp to enable contact permission", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) return } let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactPhoneNumbersKey] let request = CNContactFetchRequest(keysToFetch: keysToFetch) var cnContacts = [CNContact]() do { try store.enumerateContactsWithFetchRequest(request){ (contact, cursor) -> Void in cnContacts.append(contact) } } catch let error { NSLog("Fetch contact error: /(error)") } NSLog(">>>> Contact list:") for contact in cnContacts { let fullName = CNContactFormatter.stringFromContact(contact, style: .FullName) ?? "No Name" NSLog("/(fullName): /(contact.phoneNumbers.description)") } })

Obtener contactos es una operación lenta , por lo que no debe bloquear el hilo principal de la interfaz de usuario. Haga CNContactFetchRequest en el hilo de fondo. Es por eso que puse el código en completeHandler. Se ejecuta en un hilo de fondo.


obtenga primero el identificador de contenedor predeterminado y use el identificador de contenedor coincidente de predicado

let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey] let containerId = CNContactStore().defaultContainerIdentifier() let predicate: NSPredicate = CNContact.predicateForContactsInContainerWithIdentifier(containerId) let contacts = try CNContactStore().unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch)


@flohei responde en Swift-4

var contacts: [CNContact] = { let contactStore = CNContactStore() let keysToFetch = [ CNContactFormatter.descriptorForRequiredKeys(for: CNContactFormatterStyle.fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey, CNContactThumbnailImageDataKey] as [Any] // Get all the containers var allContainers: [CNContainer] = [] do { allContainers = try contactStore.containers(matching: nil) } catch { print("Error fetching containers") } var results: [CNContact] = [] // Iterate all containers and append their contacts to our results array for container in allContainers { let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier) do { let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch as! [CNKeyDescriptor]) results.append(contentsOf: containerResults) } catch { print("Error fetching results for container") } } return results }()


Obtenga el nombre completo, ID de correo electrónico, número de teléfono, foto de perfil y fecha de cumpleaños del marco de contactos en iOS9

#pragma mark #pragma mark -- Getting Contacts From AddressBook -(void)contactsDetailsFromAddressBook{ //ios 9+ CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { //keys with fetching properties NSArray *keys = @[CNContactBirthdayKey,CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactEmailAddressesKey]; NSString *containerId = store.defaultContainerIdentifier; NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; NSError *error; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; if (error) { NSLog(@"error fetching contacts %@", error); } else { NSString *phone; NSString *fullName; NSString *firstName; NSString *lastName; UIImage *profileImage; NSDateComponents *birthDayComponent; NSMutableArray *contactNumbersArray; NSString *birthDayStr; NSMutableArray *emailArray; NSString* email = @""; for (CNContact *contact in cnContacts) { // copy data to my custom Contacts class. firstName = contact.givenName; lastName = contact.familyName; birthDayComponent = contact.birthday; if (birthDayComponent == nil) { // NSLog(@"Component: %@",birthDayComponent); birthDayStr = @"DOB not available"; }else{ birthDayComponent = contact.birthday; NSInteger day = [birthDayComponent day]; NSInteger month = [birthDayComponent month]; NSInteger year = [birthDayComponent year]; // NSLog(@"Year: %ld, Month: %ld, Day: %ld",(long)year,(long)month,(long)day); birthDayStr = [NSString stringWithFormat:@"%ld/%ld/%ld",(long)day,(long)month,(long)year]; } if (lastName == nil) { fullName=[NSString stringWithFormat:@"%@",firstName]; }else if (firstName == nil){ fullName=[NSString stringWithFormat:@"%@",lastName]; } else{ fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName]; } UIImage *image = [UIImage imageWithData:contact.imageData]; if (image != nil) { profileImage = image; }else{ profileImage = [UIImage imageNamed:@"placeholder.png"]; } for (CNLabeledValue *label in contact.phoneNumbers) { phone = [label.value stringValue]; if ([phone length] > 0) { [contactNumbersArray addObject:phone]; } } ////Get all E-Mail addresses from contacts for (CNLabeledValue *label in contact.emailAddresses) { email = label.value; if ([email length] > 0) { [emailArray addObject:email]; } } //NSLog(@"EMAIL: %@",email); NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers",birthDayStr,@"BirthDay",email,@"userEmailId", nil]; // NSLog(@"Response: %@",personDict); [self.contactsArray addObject:personDict]; } dispatch_async(dispatch_get_main_queue(), ^{ [self.tableViewRef reloadData]; }); } } }]; }


Para Swift 4

var results: [CNContact] = [] let fetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactMiddleNameKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor,CNContactPhoneNumbersKey as CNKeyDescriptor]) fetchRequest.sortOrder = CNContactSortOrder.userDefault let store = CNContactStore() do { try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in print(contact.phoneNumbers.first?.value ?? "no") results.append(contact) }) } catch let error as NSError { print(error.localizedDescription) }

La versión anterior para resultados rápidos de var contiene todos los contactos

let contactStore = CNContactStore() var results: [CNContact] = [] do { try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactEmailAddressesKey,CNContactPhoneNumbersKey])) { (contact, cursor) -> Void in results.append(contact) } } catch{ print("Handle the error please") }


Permisos para Contactos iOS 9 SWIFT 2

let status : CNAuthorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts) if status == CNAuthorizationStatus.NotDetermined{ contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (temp: Bool, error : NSError?) -> Void in //call contacts fetching function }) }else if status == CNAuthorizationStatus.Authorized { //call contacts fetching function }) } else if status == CNAuthorizationStatus.Denied { } }


SWIFT 2

Obtener nombre completo, ID de correo electrónico, número de teléfono, imagen de perfil del marco de contactos en iOS9

NOTA También se han manejado contactos sin nombre.

Paso 1

import Contacts

Paso 2

func fetchContacts(completion: (result: NSMutableArray) -> Void ) { let finalArrayForContacts = NSMutableArray() let contactsArray = NSMutableArray() let requestForContacts = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey, CNContactFormatter.descriptorForRequiredKeysForStyle(CNContactFormatterStyle.FullName), CNContactPhoneNumbersKey ,CNContactThumbnailImageDataKey]) do{ try contactStore.enumerateContactsWithFetchRequest(requestForContacts) { (contactStore : CNContact, stop: UnsafeMutablePointer<ObjCBool>) -> Void in contactsArray.addObject(contactStore) } } catch { } if contactsArray.count > 0 { let formatter = CNContactFormatter() for contactTemp in contactsArray { let contactNew = contactTemp as! CNContact //Contact Name var stringFromContact = formatter.stringFromContact(contactNew) if stringFromContact == nil { stringFromContact = "Unnamed" } var imageData = NSData?() if contactNew.thumbnailImageData != nil{ imageData = contactNew.thumbnailImageData! }else{ // imageData = nil } var tempArray : NSArray = NSArray() if (contactNew.phoneNumbers).count > 0 { tempArray = ((contactNew.phoneNumbers as? NSArray)?.valueForKey("value").valueForKey("digits")) as! NSArray for i in 0 ..< tempArray.count { let newDict = NSMutableDictionary() let phoneNumber : String = (tempArray.objectAtIndex(i)) as! String if phoneNumber.characters.count > 0 { var test = false if phoneNumber.hasPrefix("+") { test = true } var resultString : String = (phoneNumber.componentsSeparatedByCharactersInSet(characterSet) as NSArray).componentsJoinedByString("") if test == true { resultString = "+/(resultString)" } newDict.setValue(resultString, forKey: "contact_phone") newDict.setValue(stringFromContact, forKey: "contact_name") newDict.setValue("0", forKey: "contact_select") newDict.setValue(imageData, forKey: "contact_image") finalArrayForContacts.addObject(newDict) } } }else{ // no number saved } } }else { print("No Contacts Found") } completion(result: finalArrayForContacts) }