Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
Jean-Simon Bonin edited this page Jun 11, 2015 · 7 revisions

How to

Get reference to global address book

//the variable you need to access to do anything you want is global
let addressBook : SwiftAddressBook? = swiftAddressBook
//you can store a reference to it, or access the variable every time you need it

Check for Authorization:

//ABAuthorizationStatus returned. May be changed to Swift enum in the future
//note that not the swiftAddressBook variable is accessed here, but the class
let status : ABAuthorizationStatus = SwiftAddressBook.authorizationStatus()

Request access

//success is a bool variable indicating allowance to access contacts of user
//error is a CFError object explaining possible failure
swiftAddressBook?.requestAccessWithCompletion({ (success, error) -> Void in
    if success {
      //do something with swiftAddressBook
    }
    else {
      //no success. Optionally evaluate error
    }
})

Read contacts

if let people : [SwiftAddressBookPerson]? = swiftAddressBook?.allPeople {
    //access variables on any entry of allPeople array just like always
    for person in people {
        //person.phoneNumbers is a "multivalue" entry
        //so you get an array of MultivalueEntrys
        //see MultivalueEntry in SwiftAddressBook
        let numbers = person.phoneNumbers
        //the value entry of the multivalue struct contains the data
        NSLog("%@", numbers?.map( {$0.value} ))
     }
}

Add contact

//create person in default source
//alternatively use createInSource(SwiftAddressBookSource)
var person = SwiftAddressBookPerson.create()

//set the simple properties just like usual
person.firstName = "John"
person.lastName = "Doe"
person.organization = "ACME, Inc."

//and the *multivalue* properties like this
//Multivalue property: Array of multivalue entries consisting of value, label and id.
//id does not matter on new multivalues
var email = MultivalueEntry(value: "[email protected]", label: "home", id: 0)
var email2 = MultivalueEntry(value: "[email protected]", label: "work", id: 0)
person.emails = [email, email2]
var phone = MultivalueEntry(value: "(555) 555-5555", label: "mobile", id: 0)
person.phoneNumbers = [phone]

//special on address book: you have to add the person
swiftAddressBook?.addRecord(person)
//and save the address book that changes are visible
swiftAddressBook?.save()
//alternatively, if you decide to not do the change, you can do
//swiftAddressBook?.revert()

Remove Contact

//get reference to any record first
//may it be a source, person or group
//let´s say it´s in the variable person
//that´s how you remove it:
swiftAddressBook?.removeRecord(person)
swiftAddressBook?.save()

Create group

//create group in default source
//otherwise, use createInSource(SwiftAddressBookSource)
var group = SwiftAddressBookGroup.create()

//You should give the group a name
group.name = "Testgroup"

//special on address book:
//it is necessary to save before adding people to the group
swiftAddressBook?.addRecord(group)
swiftAddressBook?.save()

//add every second person, just for testing
for var i = 0; i < self.people?.count; i += 2 {
    group.addMember(self.people![i])
}

//save again with new people contained
swiftAddressBook?.save()

Access underlying ABRecord

//get reference to any record first
//may it be a source, person or group
//let´s say it´s in the variable person
person.internalRecord