Buscar..


Observaciones

Enlaces útiles

Autorizar el acceso de contacto

Importando el framework

Rápido

import Contacts

C objetivo

#import <Contacts/Contacts.h>

Comprobando la accesibilidad

Rápido

switch CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts){
case .Authorized: //access contacts
case .Denied, .NotDetermined: //request permission
default: break
}

C objetivo

switch ([CNContactStore authorizationStatusForEntityType:CNEntityType.Contacts]){
case CNAuthorizationStatus.Authorized:
    //access contacts
    break;
case CNAuthorizationStatus.Denied:
    //request permission
    break;
case CNAuthorizationStatus.NotDetermined:
    //request permission
    break;
}

Solicitando Permiso

Rápido

var contactStore = CKContactStore()
contactStore.requestAccessForEntityType(CKEntityType.Contacts, completionHandler: { (ok, _) -> Void in
    if access{
        //access contacts
    }
}

Acceso a los contactos

Aplicando un filtro

Para acceder a los contactos, debemos aplicar un filtro de tipo NSPredicate a nuestra variable NSPredicate que definimos en el ejemplo Autorizar el acceso de contacto. Por ejemplo, aquí queremos ordenar los contactos con nombre que coincida con el nuestro:

Rápido

let predicate = CNContact.predicateForContactsMatchingName("Some Name")

C objetivo

NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"Some Name"];

Especificando claves para buscar

Aquí, queremos obtener el nombre, el apellido y la imagen del perfil del contacto:

Rápido

let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey]

Trayendo contactos

Rápido

do {
    let contacts = try contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
} catch let error as NSError {
    //...
}

Accediendo a los datos de contacto

Rápido

print(contacts[0].givenName)
print(contacts[1].familyName)
let image = contacts[2].imageData

Agregar un contacto

Rápido

import Contacts
 
// Creating a mutable object to add to the contact
let contact = CNMutableContact()
 
contact.imageData = NSData() // The profile picture as a NSData object
 
contact.givenName = "John"
contact.familyName = "Appleseed"
 
let homeEmail = CNLabeledValue(label:CNLabelHome, value:"[email protected]")
let workEmail = CNLabeledValue(label:CNLabelWork, value:"[email protected]")
contact.emailAddresses = [homeEmail, workEmail]
 
contact.phoneNumbers = [CNLabeledValue(
    label:CNLabelPhoneNumberiPhone,
    value:CNPhoneNumber(stringValue:"(408) 555-0126"))]
 
let homeAddress = CNMutablePostalAddress()
homeAddress.street = "1 Infinite Loop"
homeAddress.city = "Cupertino"
homeAddress.state = "CA"
homeAddress.postalCode = "95014"
contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]
 
let birthday = NSDateComponents()
birthday.day = 1
birthday.month = 4
birthday.year = 1988  // You can omit the year value for a yearless birthday
contact.birthday = birthday
 
// Saving the newly created contact
let store = CNContactStore()
let saveRequest = CNSaveRequest()
saveRequest.addContact(contact, toContainerWithIdentifier:nil)
try! store.executeSaveRequest(saveRequest)


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow